J
John Salerno
I'm reading about class methods now and I have a question about calling
them. I know it is wrong of me to assume it needs to work in a parallel
manner to an instance method, but I'm curious why it doesn't. Here's the
book's example:
class Multi:
def imeth(self, x):
print self, x
def cmeth(cls, x):
print cls, x
cmeth = classmethod(cmeth)
Now, when you call the instance method through the class, you say:
obj = Multi()
Multi.imeth(obj, 3)
But when you call the class method through an instance, you say:
obj.cmeth(3)
Why don't you have to explicitly pass the class name as the first
argument when you don't qualify the call with the class name? the cmeth
function has two arguments, so I would think you'd get an exception (as
you would with Multi.imeth(3)). But do class methods automatically
extract the class name from the qualifying instance and implicitly send
it to the method?
Thanks.
them. I know it is wrong of me to assume it needs to work in a parallel
manner to an instance method, but I'm curious why it doesn't. Here's the
book's example:
class Multi:
def imeth(self, x):
print self, x
def cmeth(cls, x):
print cls, x
cmeth = classmethod(cmeth)
Now, when you call the instance method through the class, you say:
obj = Multi()
Multi.imeth(obj, 3)
But when you call the class method through an instance, you say:
obj.cmeth(3)
Why don't you have to explicitly pass the class name as the first
argument when you don't qualify the call with the class name? the cmeth
function has two arguments, so I would think you'd get an exception (as
you would with Multi.imeth(3)). But do class methods automatically
extract the class name from the qualifying instance and implicitly send
it to the method?
Thanks.