What is class method?

Matthew Fitzgibbons elessar at nienna.org
Tue Aug 26 19:16:52 EDT 2008


Medardo Rodriguez (Merchise Group) wrote:
> On Tue, Aug 26, 2008 at 4:10 PM, Bruno Desthuilliers
> <bdesth.quelquechose at free.quelquepart.fr> wrote:
>> In Python, there's *no* relationship between classmethods and metaclasses.
> 
> In OOP the concept of meta-class has everything to do with class
> methods, regardless if is in Python, SmallTalk or CLOSS. "classmethod"
> decorator it's just a syntax sugar structure to define them. There is
> no difference (conceptually) on "method1" and "method2":
> <sample>
> class MetaXClass(type):
>     def Method1(self): pass
> class Xclass(object):
>     __metaclass__ = MetaXClass
>     @classmethod
>     def Method2(self): pass
> </sample>

Not quite:

 >>> class MetaXClass(type):
...     def method1(self): pass
...
 >>> class XClass(object):
...     __metaclass__ = MetaXClass
...     @classmethod
...     def method2(self): pass
...
 >>> xc = XClass()
 >>> xc.method1
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'XClass' object has no attribute 'method1'
 >>> xc.method2
<bound method MetaXClass.method2 of <class '__main__.XClass'>>
 >>>

-Matt



More information about the Python-list mailing list