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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24833338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:18:34  来源:igfitidea点击:

unbound method must be called with instance as first argument - python

pythonfunctionclasspython-2.7typeerror

提问by Rabid_Rooster

I keep on receiving the error: TypeError: unbound method get_num_students() must be called with Student instance as first argument (got nothing instead)

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

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:

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

Also, if somebody could give me help with it printing the student as a space in memory, I would also appreciate it.

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

采纳答案by Rabid_Rooster

It seems like you wanted to define grad_early, get_num_studentsand get_grad_2013as class methods, but you declared them as instance methods instead.

似乎您想将grad_early, get_num_studentsand定义get_grad_2013为类方法,但您将它们声明为实例方法。

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

不同之处在于实例方法将适用于 s = Student() s.print_name()

And a class method will work on the class itself Student.

一个类方法将在类本身Student上工作。

回答by Armsky

When you define an instance method, put a selfas the method's first argument, and when you use it, add self.in front of your instance variable.

定义实例方法的时候,把aself作为方法的第一个参数,使用的时候,self.在你的实例变量前面加上。

Like:

喜欢:

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

回答by Projesh Bhoumik

Differences in In python 2 and 3 version:

在 python 2 和 3 版本中的差异:

If you already have a default method in a class with same name and you re-declare as a same name it will appear as unbound-method call of that class instance when you wanted to instantiated it.

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

If you wanted class methods, but you declared them as instance methods instead.

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

An instance method is a method that is used when to create an instance of the class.

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

An example would be

一个例子是

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

Class label method:

类标签方法:

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

In python 2 and 3 version differ the class @classmethod to write in python 3 it automatically get that as a class-label method and don't need to write @classmethod I think this might help you.

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