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

前端 未结 3 653
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 05:51

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

Her

3条回答
  •  野性不改
    2021-02-04 06:28

    Differences in In python 2 and 3 version:

    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.

提交回复
热议问题