Yet another question on what the \'self\' is for, what happens if you don\'t use \'self\' and what\'s \'cls\' for. I \"have done my homework\", I just want to make sure I go
You use self
as the first argument in regular methods where the instance is passed automatically through this argument. So whatever the first argument is in a method - it points to the current instance
When a method is decorated with @classmethod
it gets the class passed as its first argument so the most common name for it is cls
as it points to the class.
You usually do not prefix any variables (hungarian notation is bad).
Here's an example:
class Test(object):
def hello(self):
print 'instance %r says hello' % self
@classmethod
def greet(cls):
print 'class %r greet you' % cls
Output:
>>> Test().hello()
instance <__main__.Test object at 0x1f19650> says hello
>>> Test.greet()
class greet you