[英]Python: after function call class attribute dissapears
so such a problem, that i can't figure it out. 这样的问题,我无法弄清楚。 Maybe i have too little Python knowledge.
也许我对Python的了解太少了。
The problem is, that after this function runs once smoothly, another time i get error in another function. 问题是,在该函数运行一次平稳之后,下一次我在另一个函数中遇到错误。
Function, after which things break: 功能,之后发生问题:
def setFixedPriority( self, priority, lister ):
step = priority / lister . __len__ ( )
for j in range( 0, lister . __len__( ) ):
for i in range( 0, self . listOfJobs . __len__ ( ) ) :
if self . listOfJobs[ i ] . category == lister[ j ]:
self . listOfJobs[ i ] . priority += priority
elif self . listOfJobs[ i ] . jobType == lister[ j ]:
self . listOfJobs[ i ] . priority += priority
elif self . listOfJobs[ i ] . timeToDo == lister[ j ]:
self . listOfJobs[ i ] . priority += priority
priority -= step
self . sortByPriority( )
Function in which appears the problem: 出现问题的函数:
def sortByPriority( self ) :
tmp = range ( 1, self . listOfJobs . __len__ ( ) + 1 )
for i in reversed ( tmp ) :
for j in range ( 1, i ) :
if self . listOfJobs [ j - 1 ] . priority < self . listOfJobs [ j ] . priority :
self . listOfJobs [ j - 1 ],
self . listOfJobs [ j ] = self . listOfJobst [ j ],
self . listOfJobs [ j - 1 ]
Calling the function (From different python script/file/class): 调用函数(从不同的python脚本/文件/类):
self . jobs . setFixedPriority( int( self . settings[ 'Spinbox1' ] ), self . settings[ 'type' ] . split( ":" ) )
And the error i get: 我得到的错误是:
File "data/ToDoListClass.py", line 82, in sortByPriority
self . listOfJobs [ j ] = self . listOfJobst [ j ],
AttributeError: jobList instance has no attribute 'listOfJobst'
I know that the sortByPriority works OK, because i am calling this once before setFixedPriority and it does not give me errors. 我知道sortByPriority可以正常工作,因为我在setFixedPriority之前调用过一次,并且不会给我错误。
What could cause this to happen? 什么会导致这种情况发生?
AttributeError: jobList instance has no attribute 'listOfJobst'
If you look closely, you have a typo there. 如果仔细观察,那里会有错字。 The attribute is called
listOfJobs
without a trailing t
. 该属性称为
listOfJobs
不带尾随t
。
Note that while this will make go of the error, this will probably not fix the function: 请注意,尽管这将消除错误,但可能无法修复该功能:
self . listOfJobs [ j - 1 ],
self . listOfJobs [ j ] = self . listOfJobs [ j ],
self . listOfJobs [ j - 1 ]
That construct is probably supposed to swap listOfJobs[j]
and listOfJobs[j - 1]
. 该构造可能应该交换
listOfJobs[j]
和listOfJobs[j - 1]
。 Due to the line breaks, this will do the following though: 由于换行符,这将执行以下操作:
listOfJobs[j - 1]
(nothing else happens with that) listOfJobs[j - 1]
创建一个元组( listOfJobs[j - 1]
什么也没有发生) listOfJobs[j]
to listOfJobs[j]
. listOfJobs[j]
的一个元组分配给listOfJobs[j]
。 listOfJobs[j - 1]
(again, nothing else happens). listOfJobs[j - 1]
(同样,没有其他反应)。 What you want to do is to write it either in one line: 您想要做的是将其写在一行中:
self.listOfJobs[j - 1], self.listOfJobs[j] = self.listOfJobs[j], self.listOfJobs[j - 1]
Or if you want to keep line breaks, use Python's \\
syntax to make the line continue: 或者,如果要保留换行符,请使用Python的
\\
语法使该行继续:
self.listOfJobs[j - 1], \
self.listOfJobs[j] = self.listOfJobs[j], \
self.listOfJobs[j - 1]
Although I would argue if that makes it really clear what happens. 尽管我会争论是否真的很清楚会发生什么。
Finally, you can clean your code up a lot. 最后,您可以清理很多代码。 You can directly iterate over lists, and you can also check multiple conditions in a single if.
您可以直接遍历列表,也可以在一个if中检查多个条件。 And lastly, Python's sort function allows you to specify a custom comparison function so that you don't need to implement an own sorting algorithm but can use Python's implementation.
最后,Python的sort函数允许您指定自定义比较函数,这样您就无需实现自己的排序算法,而是可以使用Python的实现。 All in all, you could for example end up with just something like this:
总而言之,例如,您可能最终会得到如下结果:
def setFixedPriority (self, priority, listers):
step = priority / len(listers)
for lister in listers:
for job in self.listOfJobs:
if job.category == lister or job.jobType == lister or job.timeToDo == lister:
job.priority += priority
priority -= step
self.listOfJobs.sort(key=lambda x: x.priority)
A few notes that will help you write more "Pythonic" code: 一些注意事项将帮助您编写更多的“ Pythonic”代码:
range()
here; range()
; you can use for ... in
constructs that are fewer lines and more clear. for ... in
行数更少,更清晰的结构中。 But that's not your main problem. sortByPriority()
should rewritten to use the built-in Python sort function; sortByPriority()
以使用内置的Python sort函数; you can give it an arbitrary function by which to sort the two comparators. sort()
. sort()
。 One thing you're doing that's bitten me a few times is that you're sorting a list while in the middle of iterating on it. 您正在做的一件事让我很痛苦,这是您在迭代列表时正在对列表进行排序。 This has caused bizarre behavior for me in the past, including breakage.
过去这对我造成了奇怪的行为,包括断裂。 This might work OK for your uses, but you should consider not sorting it until you're done tweaking everything you want in the list.
这可能适合您的使用,但是您应该考虑对列表中的所有内容进行调整之前,不要对其进行排序。
Lastly, I don't see where you create listofjobst
. 最后,我看不到您在哪里创建
listofjobst
。 Is that a typo? 那是错字吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.