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
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.
When you define an instance method, put a self
as the method's first argument, and when you use it, add self.
in front of your instance variable.
Like:
def get_num_students(self):
print "There are %s students in this school." % (self.num_students)
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.
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()
And a class method will work on the class itself Student.