所以我正在为Python编写一个程序,其中一个" User"对象被创建和操纵。
我有一个对象的链接列表,其中节点代表User
个对象。
当我将变量x
设置为User对象时,通过搜索列表并将变量设置为等于返回的Node数据,(例如x = List.search("a username")
其中search
返回具有搜索名称的用户对象)我得到一个User
对象(使用type(x)
验证)
但是,在尝试使用此User
变量上x
类的方法时,我收到NoneType错误。
可能导致这种情况的原因是什么?
如果x
直接分配给User
对象(即x = User()
),但在前一种情况下无效,则可以正常工作。
代码注释:从.txt文件中获取命令,这些命令是"添加"和#34;朋友"等等。朋友应该将每个User对象添加到另一个User对象的LinkedList中,反之亦然
代码:
class Node (object):
def __init__(self,initdata):
self.data = initdata
self.next = None # always do this – saves a lot
# of headaches later!
def getData (self):
return self.data # returns a POINTER
def getNext (self):
return self.next # returns a POINTER
def setData (self, newData):
self.data = newData # changes a POINTER
def setNext (self,newNext):
self.next = newNext # changes a POINTER
class UnorderedList ():
def __init__(self):
self.head = None
def isEmpty (self):
return self.head == None
def add (self,item):
# add a new Node to the beginning of an existing list
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def length (self):
current = self.head
count = 0
while current != None:
count += 1
current = current.getNext()
return count
def search (self,item): #NOW RETURNS OBJECT IN LIST
current = self.head
while current != None:
if current.getData().name == item:
return current.getData()
else:
current = current.getNext()
return None
def remove (self,item):
current = self.head
previous = None
found = False
while not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext() )
class User():
def __init__(self):
self.name = ""
self.friendsList = UnorderedList()
def setName(self,info):
self.name = info
def getName(self):
return self.name
def removeFriend(self, item):
self.friendsList.remove(item)
def addFriend(self, item):
self.friendsList.add(item)
def searchList(self, item):
self.friendsList.search(item)
def __str__(self):
return self.name
def main():
inFile = open("FriendData.txt")
peopleList = UnorderedList()
for line in inFile:
textList = line.split()
if "Person" in textList:
newUser = User()
newUser.setName(textList[1])
if peopleList.search(newUser.getName()) != None:
print("This user is already in the program")
else:
peopleList.add(newUser)
print(newUser.getName(),"now has an account")
elif "Friend" in textList:
#PROBLEM OBJECTS a AND b BELOW
a = peopleList.search(textList[1]) #returns user1 object
b = peopleList.search(textList[2]) # return user2 object
b.getName()
if peopleList.search(textList[1]) == None:
print("A person with the name", textList[1], "does not currently exist")
elif peopleList.search(textList[2]) == None:
print("A person with the name", textList[2], "does not currently exist")
elif textList[1]==textList[2]:
print("A person cannot friend him/herself")
elif peopleList.search(textList[1]).searchList(textList[2])!= None:
print(textList[1],"and",textList[2],"are already friends")
elif peopleList.search(textList[2]).searchList(textList[1]) != None:
print(textList[2],"and",textList[1],"are already friends")
#else:
#a.friendsList.add(b) #adds user 2 to user1 friendlist
#b.addFriend(a)
#print(a.getName(),"and",b.getName(),"are now friends")
main()
错误:
Traceback (most recent call last):
File "C:/Users/rsaen/Desktop/Python Prgms/Friends.py", line 137, in <module>
main()
File "C:/Users/rsaen/Desktop/Python Prgms/Friends.py", line 119, in main
b.getName()
AttributeError: 'NoneType' object has no attribute 'getName'
解析.txt文件
['Person', 'Rachel']
['Person', 'Monica']
['Friend', 'Rachel', 'Monica']
['Friend', 'Monica', 'Rachel']
['Friend', 'Rachel', 'Ross']
['Person', 'Ross']
['Friend', 'Rachel', 'Ross']
['Person', 'Joey']
['Person', 'Joey']
['Friend', 'Joey', 'Joey']
['Friend', 'Joey', 'Rachel']
['Person', 'Chandler']
['Friend', 'Chandler', 'Monica']
['Friend', 'Chandler', 'Rachel']
['Friend', 'Ross', 'Chandler']
['Friend', 'Phoebe', 'Rachel']
['Person', 'Phoebe']
['Friend', 'Phoebe', 'Rachel']
['Exit']
答案 0 :(得分:0)
从代码和错误消息中看起来好像textList[2]
属性中没有friendsList
。事实上,它看起来不会;看起来User
属性中唯一的friendsList
将是textList[1]
,因为friendsList
在没有任何参数的情况下被实例化,之后只会添加textList[1]
。< / p>
重要的是要知道:Python中没有指针。有关详情,请参阅此处:https://ubuntuforums.org/showthread.php?t=895804
同样重要的是要知道:以下是如何在Python中测试对象是否为None
:
if x is not None:
print(x)
答案 1 :(得分:0)
问题在于,在你输入的第5行,你试图在添加Ross之前添加Rachel和Ross之间的友谊。该程序找不到Ross,变量b
为None
,b.getName()崩溃。
我注意到两件事:在User类的搜索方法中,您需要从搜索中返回结果。我也看到你使用的是getter和setter,它们可能是赋值的一部分(?),但Python中不需要这些,因为它有一种更温和的封装形式。