Can __iter__ be used as a classmethod?

Bjorn Pettersen BPettersen at NAREX.com
Tue Mar 4 16:06:20 EST 2003


> From: Michele Simionato [mailto:mis6 at pitt.edu] 
> 
[...]

> Is there any good reason for supporting classmethods in Python ?

Yes, classmethods can do everything staticmethods can, plus
polymorphism. E.g.:

>>> class meta(type):
...   def call_f(cls): cls.f()
...
>>> class A(object):
...   __metaclass__ = meta
...   def f(cls): print 'A.f'
...   f = classmethod(f)
...
>>> class B(object):
...   __metaclass__ = meta
...   def f(cls): print 'B.f'
...   f = classmethod(f)
...
>>> A.call_f()
A.f
>>> B.call_f()
B.f
>>>

Digressing: It never occurred to me that assignment to __metaclass__ was
defining the class of your class until it was pointed out earlier today.
That made everything much clearer :-)

-- bjorn





More information about the Python-list mailing list