未绑定的方法必须调用实例作为第一个参数 - python [英] unbound method must be called with instance as first argument - python
问题描述
我一直收到错误:TypeError:未绑定的方法get_num_students()必须调用Student实例作为第一个参数(没有变化)
我需要这个项目做的今晚...
任何和所有的帮助是很好的赞赏。
这是代码:
class Student(object):
num_students = 0
num_grad_2013 = 0
def __init __(self,first_name,last_name ,id_num,yr_of_grad,顾问):
self = self
self.first_name = first_name
self.last_name = last_name
self.id_num = int(id_num)
self .yr_of_grad = int(yr_of_grad)
self.counselor =顾问
def to_string(first_name,last_name,id_num,yr_of_grad,顾问):
print first_name
print last_name
print id_num
print yr_of_grad
打印顾问
def move():
num_students - = 1
如果yr_of_grad = = 12:
num_grad_2013 - = 1
else:
无
print学生ID编号:%s已移动。 %(id_num)
def grad_early():
num_students - = 1
num_grad_2013 - = 1
printID号为%s的学生提前毕业。 。 %(id_num)
def get_num_students():
print这所学校有%s的学生。 %(num_students)
def get_grad_2013():
print今年有%s的学生毕业。 %(num_grad_2013)
def main():
print创建学生Nathan Lindquist
nathan =学生(Nathan,Lindquist,11111,2014,Iverson )
print nathan
print创建学生Dylan Schlact
dylan =学生(Dylan,Schlact,22222,2012,Greene)
print dylan
print创建学生Matt Gizzo
matt =学生(Matt,Gizzo,33333,2013,Connor)
print matt
# 3,一个是在2013毕业
Student.get_num_students()
Student.get_grad_2013()
#改变一些东西!
nathan.grad_early()
print nathan
matt.move()
#matt.grad_early()
#print matt
#so是2,一个是在2013毕业
Student.get_num_students()
Student.get_grad_2013()
return
这里是Python输出:
>> main()
创建学生Nathan Lindquist
< __ main __。在0x03065430的学生对象>
创建学生Dylan Schlact
< __ main __。在0x030653B0的学生对象>
创建学生Matt Gizzo
< __ main __。在0x030653D0的学生对象>
回溯(最近最后一次调用):
在< module>中的文件< pyshell#8>
main()
文件C:\Users\admin\Desktop\Python\student.py,第51行,在主
Student.get_num_students()
TypeError:未绑定的方法get_num_students()必须用Student实例作为第一个参数调用(没有改变)
<
谢谢!
$ 你似乎想要定义 grad_early
, get_num_students
/ code>和 get_grad_2013
作为类方法,但是将它们声明为实例方法。
实例方法是一个属于该类实例的方法。
一个例子是
类Student(object):
#...
def print_name(self):#这是一个实例方法
print执行实例方法
@classmethod
def num_of_students(cls)
print执行类方法
不同的是,一个实例方法将用于
s = Student()
s.print_name()
类方法将在类本身上工作
Student。
I keep on receiving the error: TypeError: unbound method get_num_students() must be called with Student instance as first argument (got nothing instead)
I need to have this project done by tonight... Any and all help is well appreciated.
Here is the code:
class Student(object):
num_students = 0
num_grad_2013 = 0
def __init__(self, first_name, last_name, id_num, yr_of_grad, counselor):
self = self
self.first_name = first_name
self.last_name = last_name
self.id_num = int(id_num)
self.yr_of_grad = int(yr_of_grad)
self.counselor = counselor
def to_string(first_name, last_name, id_num, yr_of_grad, counselor):
print first_name
print last_name
print id_num
print yr_of_grad
print counselor
def move():
num_students -= 1
if yr_of_grad == 12:
num_grad_2013 -= 1
else:
None
print "Student with ID number: %s has moved." % (id_num)
def grad_early():
num_students -= 1
num_grad_2013 -= 1
print "Student with ID number: %s is graduating early." % (id_num)
def get_num_students():
print "There are %s students in this school." % (num_students)
def get_grad_2013():
print "There are %s students graduating this year." % (num_grad_2013)
def main():
print "Creating student Nathan Lindquist"
nathan = Student("Nathan", "Lindquist", 11111, 2014, "Iverson")
print nathan
print "Creating student Dylan Schlact"
dylan = Student("Dylan", "Schlact", 22222, 2012, "Greene")
print dylan
print "Creating student Matt Gizzo"
matt = Student("Matt", "Gizzo", 33333, 2013, "Connor")
print matt
# so number of students is 3, one is graduating in 2013
Student.get_num_students()
Student.get_grad_2013()
# change some things!
nathan.grad_early()
print nathan
matt.move()
#matt.grad_early()
#print matt
# so number of students is 2, one is graduating in 2013
Student.get_num_students()
Student.get_grad_2013()
return
Here is the Python output:
>>> main()
Creating student Nathan Lindquist
<__main__.Student object at 0x03065430>
Creating student Dylan Schlact
<__main__.Student object at 0x030653B0>
Creating student Matt Gizzo
<__main__.Student object at 0x030653D0>
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
main()
File "C:\Users\admin\Desktop\Python\student.py", line 51, in main
Student.get_num_students()
TypeError: unbound method get_num_students() must be called with Student instance as first argument (got nothing instead)
Also, if somebody could give me help with it printing the student as a space in memory, I would also appreciate it!
Thank you!
It seems like you wanted to define grad_early
, get_num_students
and get_grad_2013
as class methods, but you declared them as instance methods instead.
An instance method is a method that, well, belongs to an instance of the class.
An example would be
class Student(object):
# ...
def print_name(self): # This is an instance method
print "executing instance method"
@classmethod
def num_of_students(cls)
print "executing class method"
The difference is that an instance method will work on s = Student() s.print_name()
And a class method will work on the class itself Student.
这篇关于未绑定的方法必须调用实例作为第一个参数 - python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!