calling a class method

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top