gpt4 book ai didi

Python AttributeError 'str' 对象没有属性 '_sID'

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:42 24 4
gpt4 key购买 nike

我在 python ecplise 中遇到错误,但没有找到解决方案。

类(class):

class Student:
'''
This class is used to store data about the students
It contains:
sID - id of student
sName - name of student
'''
def __init__(self, sID, sName):
'''
Initialized the student
'''
self._sID = sID
self._sName = sName


def getID(self):
'''
Return student id
'''
return self._sID

def setID(self, ID):
self._sID = ID

def setName(self, name):
self._sName = name

def getName(self):
'''
Return student name
'''
return self._sName

def __str__(self):
'''
Converts the student into printable text
'''
msg ='ID: ' + str(self._sID) + ', Name: ' + self._sName
return msg

def __eq__(self, s):
'''
Checks if two students have the same ID and name
'''
return self._sID == s._sID and self._sName == s._sName

以下是属性错误:

 Traceback (most recent call last):
File "C:\Users\crist\workspace\lab5_7\appStart.py", line 16, in <module>
ui.mainMenu()
File "C:\Users\crist\workspace\lab5_7\UI\ui.py", line 80, in mainMenu
self._searchElementMenu(cmd[1])
File "C:\Users\crist\workspace\lab5_7\UI\ui.py", line 57, in _searchElementMenu
self._controller.searchElement(cType, cSearch)
File "C:\Users\crist\workspace\lab5_7\controller\controller.py", line 27, in searchElement
if isinstance(lst[i], Student) == True and lst[i] == eSearch:
File "C:\Users\crist\workspace\lab5_7\domain\student.py", line 55, in __eq__
return self._sID == s._sID and self._sName == s._sName
AttributeError: 'str' object has no attribute '_sID'

有人可以帮助我吗?

如果有必要,我可以给你更多代码。

sID是每个学生的唯一id,我需要这个函数来验证是否有更多学生具有相同的id。

非常感谢!

最佳答案

您正在尝试将 = 运算符与 Student 实例和字符串一起使用。

该错误表明字符串实例没有 _sID 变量,例如:

"test"._sID

关于Python AttributeError 'str' 对象没有属性 '_sID',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33597212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com