What is class method?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Aug 26 22:10:51 CEST 2008


Medardo Rodriguez (Merchise Group) a écrit :
> On Sun, Aug 24, 2008 at 4:32 AM, Hussein B <hubaghdadi at gmail.com> wrote:
>> I'm familiar with static method concept, but what is the class method?
>> how it does differ from static method? when to use it?
> 
> "Class Methods" are related to the meta-class concept introduced since
> the first beginning of OOP but not known enough so far.
> If you are capable to reason (to model) using that concept, the you
> will need "classmethod" decorator in Python.

In Python, there's *no* relationship between classmethods and 
metaclasses. A classmethod is just a descriptor class attribute that act 
as a wrapper around a function in such a way that when the attribute is 
looked up (weither on the class or an instance of it) yields another 
(callable) wrapper around the function and the class, this last wrapper 
being responsible for calling the function with the class itself as 
first argument.

> "Static Methods" are global operations but are declared in the
> name-space context of a class; so, they are not strictly methods.
> 
> In Python everything is an object, but functions declared in the
> module scope not receive the instance of the module, so they are not
> module methods, they are not methods, they are global functions

You can drop the 'global' - there's nothing like a real global scope in 
Python.

> that
> are in the name-space context of the module in this case.
> 
> Methods always receive the instance as a special argument (usually
> declared as self in Python).

Nope. Functions wrapped by a method object receive the instance as 
*first* (not 'special') argument. In Python, a method is only a wrapper 
object around the function, the class and the instance. This wrapper is 
built by the function object's __get__ method (descriptor protocol) each 
time the attribute is looked up.

> Classes (theoretically speaking) are also
> objects 

Why "theoretically speaking" ?

 >>> class Foo(object): pass
...
 >>> isinstance(Foo, object)
True
 >>>

Python's classes *are* objects.


> (dual behavior).
> 
> Let's be explicit:
> 
> #<code>
> class Test(object):
>    def NormalMethod(self):
>        print 'Normal:', self

<ot>
pep08 : method names should be all_lower
</ot>

>    @staticmethod
>    def StaticMethod(self=None):
>        print 'Static:', self
> 
>    @classmethod
>    def ClassMethod(self):
>        print 'Class:', self

<ot>
The convention for classmethod is to name the first function argument 'cls'.
</ot>


> test = Test()
> test.NormalMethod()
> test.StaticMethod()
> test.ClassMethod()   # the instance "test" is coerced to it's class to
> call the method.

Nope. There's nothing like "coercion" in Python. It's just that the 
descriptor protocol pass both the instance and it's class (or only the 
class if the lookup happens on the class itself) to the __get__ method, 
so the classmethod's __get__ method can store a reference to it and use 
that reference as first arg when calling the wrapped function.

If you really intend to go into low-level explanations of Python's 
object model, please get your facts right first. It's already difficult 
enough to explain, no need to add misleading or inexact stuff.



More information about the Python-list mailing list