Can __iter__ be used as a classmethod?

Alex Martelli aleax at aleax.it
Tue Mar 4 17:17:20 EST 2003


Michele Simionato wrote:
   ...
> Anyway, I can emulate classmethods with regular methods in the
> metaclass:
> 
>>>> class meta(type):
> ...   def call_f(cls): cls.f() # a perverse way of calling f
> ...   def f(cls): print "%s.f" % cls.__name__
> 
>>>> class A(object):
> ...   __metaclass__ = meta
> ...
>>>> class B(A): pass #notice: B inherits the metaclass of A
> ...
> 
>>>> A.call_f()
> A.f
>>>> B.call_f()
> B.f

Not quite -- try A().call_f()... would work just fine if call_f
were a static method, but won't work for a metaclass method.

Not a decisive advantage, but there can be some elegance in
being able to call A.f() and A().f() similarly.


Alex





More information about the Python-list mailing list