【问题标题】:unbound method must be called with instance as first argument - python必须以实例作为第一个参数调用未绑定的方法 - python
【发布时间】:2014-09-10 02:00:32
【问题描述】:

我不断收到错误消息: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)

另外,如果有人可以帮助我将学生打印为内存空间,我也将不胜感激。

【问题讨论】:

  • 您的代码有很多问题。您应该阅读the Python tutorial 以了解如何使用类。
  • 只需为方法定义中的每个第一个参数添加 self 关键字:def yourmethod(self, ...)
  • BrenBarn,我知道它有问题......这是我的第一段代码,其中包含类。另外,Felipsmartins,我不太明白你的意思。
  • @Rabid_Rooster 我假设你很久以前就通过了。所以请原谅我唤醒了这个帖子,但我想把这个留在这里,让下一个新手来。就像 BrenBarn 和 felipsmartins 说的那样,看的地方在这里:docs.python.org/2/tutorial/classes.html9.3.2。

标签: python function class python-2.7 typeerror


【解决方案1】:

您似乎想将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 = 学生() s.print_name()

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

【讨论】:

  • 新错误:回溯(最近一次调用最后一次):文件“”,第 1 行,在 main() 文件“C:\Users\admin\Desktop\Python \student.py”,第 70 行,主要 nathan.grad_early() 文件“C:\Users\admin\Desktop\Python\student.py”,第 42 行,在 grad_early 中打印“学生 ID 号:%s 即将毕业早期的。” % (id_num) NameError: 全局名称 'id_num' 未定义
【解决方案2】:

python 2 和 3 版本的区别:

如果您在同名的类中已经有一个默认方法,并且您重新声明为同名,那么当您想要实例化它时,它将显示为该类实例的未绑定方法调用。

如果您想要类方法,但您将它们声明为实例方法。

实例方法是在创建类的实例时使用的方法。

一个例子是

   def user_group(self):   #This is an instance method
        return "instance method returning group"

类标签方法:

   @classmethod
   def user_group(groups):   #This is an class-label method
        return "class method returning group"

在 python 2 和 3 版本不同的类@classmethod 来写 在 python 3 中,它会自动将其作为类标签方法获取,并且不需要编写 @classmethod 我想这可能会对你有所帮助。

【讨论】:

    【解决方案3】:

    定义实例方法时,将self作为方法的第一个参数,使用时,在实例变量前添加self.

    喜欢:

    def get_num_students(self):
        print "There are %s students in this school." % (self.num_students)
    

    【讨论】:

    • num_studends 显然是一个类属性,而不是一个实例(即学生)属性
    猜你喜欢
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 2018-05-10
    • 2015-11-07
    • 1970-01-01
    相关资源
    最近更新 更多