how to tell a method is classmethod or static method or instancemethod

S

Steven D'Aprano

how to tell a method is class method or static method or instance
method?

That's a good question, with a subtle answer that depends on exactly what
you mean by the question. If you mean the object you get back from
ordinary attribute access like "instance.method", then you do this:
.... @classmethod
.... def cmethod(cls):
.... pass
.... @staticmethod
.... def smethod():
.... pass
.... def method(self):
.... pass
....<type 'function'>

So static methods are just functions, and both class methods and instance
methods share the same underlying type:
<type 'instancemethod'>


But if you dig deeper, you learn that all methods are actually
descriptors:
type(K.__dict__['cmethod'])
type(K.__dict__['smethod'])
type(K.__dict__['method'])
<type 'function'>

(Functions are descriptors too.)

This is deep magic in Python, but if you want to learn more about it, you
can read this:

http://users.rcn.com/python/download/Descriptor.htm


And I'll take this opportunity to plug my dualmethod descriptor:

http://code.activestate.com/recipes/577030-dualmethod-descriptor/
 
8

88888 Dihedral

在 2012å¹´2月13日星期一UTC+8下åˆ4æ—¶03分24秒,Steven D'Aprano写é“:
how to tell a method is class method or static method or instance
method?

That's a good question, with a subtle answer that depends on exactly what
you mean by the question. If you mean the object you get back from
ordinary attribute access like "instance.method", then you do this:
... @classmethod
... def cmethod(cls):
... pass
... @staticmethod
... def smethod():
... pass
... def method(self):
... pass
... <type 'function'>

So static methods are just functions, and both class methods and instance
methods share the same underlying type:
<type 'instancemethod'>


But if you dig deeper, you learn that all methods are actually
descriptors:
type(K.__dict__['cmethod'])
type(K.__dict__['smethod'])
type(K.__dict__['method'])
<type 'function'>

(Functions are descriptors too.)

This is deep magic in Python, but if you want to learn more about it, you
can read this:

http://users.rcn.com/python/download/Descriptor.htm


And I'll take this opportunity to plug my dualmethod descriptor:

http://code.activestate.com/recipes/577030-dualmethod-descriptor/

The methods of an object can be well organized
to avoid redundant checks of operations
desired to be performed on the object.

Also an object's methods should be factored to be easy to be maintained
and to provide debugging and error information for the programmer to track
the operations related to the hardware, the OS, and the sofware design
issues.
 

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

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top