必须以实例作为第一个参数调用未绑定方法-python

Rabid_Rooster

我继续收到错误: TypeError: unbound method get_num_students() must be called with Student instance as first argument (got nothing instead)

这是代码:

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

这是Python输出:

>>> 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)

另外,如果有人可以帮助我将学生打印为记忆中的空白,我也将不胜感激。

用户名

好像你想定义grad_earlyget_num_studentsget_grad_2013为类方法,但是你宣布他们作为实例方法来代替。

实例方法是属于类的实例的方法。

一个例子是

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"

区别在于实例方法将适用于s = Student()s.print_name()

并且班级方法将对班级本身Student起作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

未绑定的方法f()必须以fibo_实例作为第一个参数调用(取而代之的是class classobj实例)

每次在Python类中是否都必须将__init__作为第一个函数?

我可以将self作为python中类方法的第一个参数传递吗

在Python中创建namedtuple时,为什么必须提供typename作为第一个参数

必须以实例作为第一个参数调用未绑定方法

TypeError-未绑定方法ToTransition()必须以FSM实例作为第一个参数来调用(取而代之的是str实例)

必须以MyModel实例作为第一个参数来调用未绑定方法save()

TypeError:必须以LoginPage实例作为第一个参数来调用未绑定方法test_logn()(改为使用CompanyManagement实例)

TypeError:未绑定方法parse()必须以ElementTree实例作为第一个参数调用(改为使用str str实例)

TypeError:未绑定方法strftime()必须以Form1的第38行的日期时间实例作为第一个参数(而不是got str实例)来调用

如何将AWK命令输出作为第一个参数传递给Python

错误:必须使用“类名”实例作为第一个参数来调用未绑定方法“方法名”(取而代之的是classobj实例)

未绑定的方法必须以实例作为第一个参数来调用

未绑定的方法equaldigits()必须以sut实例作为第一个参数来调用(取而代之的是int实例)

必须使用流处理程序实例作为fistr参数调用python typeerror未绑定方法发出(改为使用filehandler实例)

TypeError:未绑定方法move_to_element()必须以ActionChains实例作为第一个参数来调用(取而代之的是获取列表实例)

如何修复TypeError:必须使用Dropper实例作为第一个参数来调用未绑定方法

DatabaseError:要执行的第一个参数必须是python中的字符串或Unicode查询

必须使用GetPostView实例作为第一个参数来调用未绑定的方法comment()(改为使用WSGIRequest实例)

在python中查找单词的第一个实例

类型错误:必须使用 Link 实例作为第一个参数调用未绑定的方法 set_rank()(什么都没有)

必须使用 Tk 实例作为第一个参数调用未绑定的方法 mainloop()(什么都没有)

类型错误:必须使用 Vars 实例作为第一个参数调用未绑定的方法 SendVars()(改为使用 bool 实例)

第一个参数是 class 或 self 的 Python 方法

必须使用 UserLoginForm 实例作为第一个参数调用未绑定的方法 is_valid() (什么都没有)

Python 错误:必须使用 Point 实例作为第一个参数调用未绑定的方法 distance()(改为使用 classobj 实例)

Python3:可以使用“self”以外的方法作为方法的第一个参数吗?

Python Pandas:TypeError:第一个参数必须是用户定义函数中的字符串或编译模式

第一个参数必须是函数的可调用 python 调度