问题
I have a set of python files which make up a program for saving tidbits of info and searching them via tag association. I had the program working for many versions, but I recently made what I thought was a minor change that caused the program to go haywire. The ENTRY object is the basis of the data storage - it holds a unique ID number, a nickname, a value, and a list of tags.
class ENTRY:
def __init__(self, idNum, nickName, value, tagList):
self.idNum = idNum
self.nickName = nickName
self.value = value
self.tagList = tagList
I realized that I was interchangeably referring to the "nickName" attribute as "name" when asking for input in other files, so I decided to Find and Replace all mentions of "nickName" with "name" to make the code easier to follow. I did this in the ENTRY.py file as well as all of the associated python files in the program. I even proofread them to make sure the change did not mess with any case-sensitive function calls or anything.
The problem: Now, when I run the program, I get an attribute error:
Traceback (most recent call last):
File "/Memory/TagMem.py", line 210, in <module>
main()
File "/Memory/TagMem.py", line 207, in main
dispatch(userChoice)
File "/Memory/TagMem.py", line 171, in dispatch
nameList('todo')
File "/Memory/TagMem.py", line 103, in nameList
memory.searchListNames(queryList)
File "/Memory/Memory.py", line 96, in searchListNames
each.printName()
File "/Memory/ENTRY.py", line 49, in printName
print("({}) {}".format(self.idNum, self.name))
AttributeError: 'ENTRY' object has no attribute 'name'
But after the Find and Replace, the ENTRY object most certainly has an attribute 'name':
class ENTRY:
def __init__(self, idNum, name, value, tagList):
self.idNum = idNum
self.name = name
self.value = value
self.tagList = tagList
Does anyone know of a reason I would get an attribute error when the attribute is very clearly defined in the class constructor?
For more complete information on the full class code, see the github repository: https://github.com/kylelambert101/TagMem
And the specific commit with the crash-inducing changes: https://github.com/kylelambert101/TagMem/commit/68987f2e6ed98012f36ec5230b3dac6f09373489
Thank you!
回答1:
I figured out where I went wrong! My program is associated with a file called myMemory.dat
which stores a pickled version of a Memory
object and is loaded and saved with each run of the program. The Memory
object is essentially a list of ENTRY
s to be queried. At the time when I updated all of my code to use the name name
instead of nickname
, I already had hundreds of ENTRY
objects saved in myMemory.dat
- each an instance of the old ENTRY
object with a nickName
attribute instead of a name
attribute. When I called functions which attempted to access the ENTRY
object's name
attribute, the program threw an error because the ENTRY
objects in question did not have that attribute.
The fix: I looped through the Memory
object stored in myMemory.dat
and copied all of the info into new ENTRY
s in a new Memory
object. I saved the new Memory
to myMemory.dat
, and the program works good as new!
来源:https://stackoverflow.com/questions/38018475/python-3-attributeerror-even-though-attribute-exists