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

标签 python function class python-2.7 typeerror

我一直收到错误: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.print_name()

类方法将作用于类本身 学生。

关于python - 必须以实例作为第一个参数调用未绑定(bind)方法 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24833338/

相关文章:

android - 我无法通过谷歌云消息在我的 Android 设备上接收消息?

python - 模拟 - 测试是否在不指定参数的情况下调用方法

c++ - C++ 中两个非常相似的类,只有一种不同的方法 : how to implement?

c++ - 成员(member)名查找规则

python - 查找一定范围内有间隙的子列表

javascript - 我应该将函数放在 Javascript 代码中的什么位置?

无参数传递或返回值的 C 程序汇编函数

javascript - VideoJS v5 - 在 ES6 中添加自定义组件 - 我做得对吗?

python - 如何在子类的 __init__() 中附加另一个参数?

python - 我无法在 python 中使用 matplotlib 在 close() 之后调用 show()