As a side observation, note that Pilgrim is committing a common misuse of terms here: a class method is quite a different thing from an instance method, which is what he's talking about here. As wikipedia puts it, "a method is a subroutine that is exclusively associated either with a class (in which case it is called a class method or a static method) or with an object (in which case it is an instance method).". Python's built-ins include a staticmethod
type, to make static methods, and a classmethod
type, to make class methods, each generally used as a decorator; if you don't use either, a def
in a class body makes an instance method. E.g.:
>>> class X(object):
... def noclass(self): print self
... @classmethod
... def withclass(cls): print cls
...
>>> x = X()
>>> x.noclass()
<__main__.X object at 0x698d0>
>>> x.withclass()
<class '__main__.X'>
>>>
As you see, the instance method noclass
gets the instance as its argument, but the class method withclass
gets the class instead.
So it would be extremely confusing and misleading to use self
as the name of the first parameter of a class method: the convention in this case is instead to use cls
, as in my example above. While this IS just a convention, there is no real good reason for violating it -- any more than there would be, say, for naming a variable number_of_cats
if the purpose of the variable is counting dogs!-)