简体   繁体   English

必须使用instance作为第一个参数调用unbound方法 - python

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

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. 另外,如果有人能帮我把它打印成记忆中的空间,我也会很感激。

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. 您似乎想要将grad_earlyget_num_studentsget_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上工作。

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版本中,不同类@classmethod在python 3中编写它会自动将其作为类标签方法,并且不需要编写@classmethod我认为这可能会对你有所帮助。

When you define an instance method, put a self as the method's first argument, and when you use it, add self. 定义实例方法时,将self作为方法的第一个参数,当您使用它时,添加self. in front of your instance variable. 在你的实例变量前面。

Like: 喜欢:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python TypeError:必须以实例作为第一个参数调用未绑定方法 - Python TypeError: unbound method must be called with instance as first argument Python错误“必须以实例作为第一个参数来调用未绑定方法” - Python error “unbound method must be called with instance as first argument” 必须使用instance作为第一个参数调用unbound方法 - unbound method must be called with instance as first argument python未绑定方法必须 <myMethod> 必须与 <MyFrame> 实例作为第一个参数(改为什么也不做) - python unbound method must <myMethod> must be called with <MyFrame> instance as first argument (got nothing instead) Python-TypeError:未绑定的方法beamDeflection()必须以Beam实例作为第一个参数调用(取而代之的是获取列表实例) - Python - TypeError: unbound method beamDeflection() must be called with beam instance as first argument (got list instance instead) TypeError:必须使用实例作为第一个参数(在Python 2中使用int实例)调用unbound方法 - TypeError: unbound method must be called with instance as first argument (got int instance instead) in Python 2 Python:必须使用MessageNet实例作为第一个参数调用未绑定方法receive()(改为使用MessageGlobal实例) - Python :unbound method receive() must be called with MessageNet instance as first argument (got MessageGlobal instance instead) 是什么导致“未绑定的方法__init __()必须使用实例作为第一个参数调用”来自此Python代码? - What is causing “unbound method __init__() must be called with instance as first argument” from this Python code? python - 必须使用实例作为第一个参数调用未绑定的方法(什么都没有) - python - unbound method must be called with instance as first argument (got nothing instead) Python-TypeError:必须使用Label实例作为第一个参数来调用未绑定方法configure()(而是什么也不做) - Python-TypeError: unbound method configure() must be called with Label instance as first argument (got nothing instead)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM