Python 参数'没有值;得分';在未绑定方法调用中
我一直在尝试创建一个类,如下所示:Python 参数'没有值;得分';在未绑定方法调用中,python,python-3.x,visual-studio-code,Python,Python 3.x,Visual Studio Code,我一直在尝试创建一个类,如下所示: class Student: numberOfStudents = 0 numberofsections = 0 def __init__(self): print("Welcome to our humble organization.") def RegisterStudent(self, name, age, phonenumber, score): self.nam
class Student:
numberOfStudents = 0
numberofsections = 0
def __init__(self):
print("Welcome to our humble organization.")
def RegisterStudent(self, name, age, phonenumber, score):
self.name = name
self.age = age
self.number = phonenumber
self.score = score
Student.numberOfStudents += 1
return "Registeration is done successfully"
def ViewStudentData(self):
return f"Student name: {self.name}\nStudent age: {self.age}\nStudent score: {self.score}"
@classmethod
def Number_Of_Students(cls):
return f"The number of students is: {Student.numberOfStudents}"
@classmethod
def Number_Of_Sections(cls):
Student.numberofsections = (Student.numberOfStudents/30)
Student.numberofsections = int(Student.numberofsections)
return f"Numebr of sections is {Student.numberofsections}"
@classmethod
def View_year_Data(cls):
return Student.Number_Of_Students(), Student.Number_Of_Sections()
Std_One = Student.RegisterStudent("Yakoza", 21, "5465164641564", 80)
当我尝试调用一个实例方法时,如下所示:
class Student:
numberOfStudents = 0
numberofsections = 0
def __init__(self):
print("Welcome to our humble organization.")
def RegisterStudent(self, name, age, phonenumber, score):
self.name = name
self.age = age
self.number = phonenumber
self.score = score
Student.numberOfStudents += 1
return "Registeration is done successfully"
def ViewStudentData(self):
return f"Student name: {self.name}\nStudent age: {self.age}\nStudent score: {self.score}"
@classmethod
def Number_Of_Students(cls):
return f"The number of students is: {Student.numberOfStudents}"
@classmethod
def Number_Of_Sections(cls):
Student.numberofsections = (Student.numberOfStudents/30)
Student.numberofsections = int(Student.numberofsections)
return f"Numebr of sections is {Student.numberofsections}"
@classmethod
def View_year_Data(cls):
return Student.Number_Of_Students(), Student.Number_Of_Sections()
Std_One = Student.RegisterStudent("Yakoza", 21, "5465164641564", 80)
它给了我这个错误:
No value for argument 'score' in unbound method call pylint(no-value-for-parameter)
错误在哪里,我看不到它(用Dora的声音)通过将它称为
Student.RegisterStudent(…)
,您正在使用RegisterStudent
作为类方法,即使您想创建Student
的实例。简单的解决方法是:
StdOne = Student().RegisterStudent("Yakoza", 21, "5465164641564", 80)
该Student()
部分创建类的实例,然后将其作为变量self
传递给RegisterStudent
。(您收到错误是因为在最初的调用中,self=“Yakoza”
,name=21
,age=“5465164641564”
和phonenumber=80
;因此score
没有得到值。根本原因是调用它的方式。)
但是,RegisterStudent
返回文本“Registration is done successfully”,该文本对于存储在变量中没有用处。将返回行更改为returnself
,该对象是通过调用Student()
创建的
之后,您可以看到它通过以下方式工作:
>>> print(StdOne.name)
'Yakoza'
>>> print(Student.numberOfStudents)
1
顺便说一句,您应该初始化
\uuu init\uuu()
中的所有实例变量,即使只是将它们设置为None
,因为Python3优化了实例字典,如果\uu init\uuu()
中声明了所有实例变量。因此:
class Student:
numberOfStudents = 0
numberofsections = 0
def __init__(self):
self.name = None
self.age = None
self.number = None
self.score = None
print("Welcome to our humble organization.")
我不知道该说什么,我非常感谢你,伙计,不仅仅是因为你的回答,还有额外的提示❤❤❤您可以使用
RegisterStudent
作为一个类方法,就像您为其他人所做的那样:将self
更改为cls
,在它内部,您可以执行inst=cls()
操作,该操作将创建Student
的实例,然后执行inst.name=…
等操作,然后执行返回inst
。然后像最初那样称呼它。顺便说一句,如果你对答案感到满意,请阅读“”,以及关于和的内容。