Can __iter__ be used as a classmethod?
Samuele Pedroni
pedronis at bluewin.ch
Tue Mar 4 13:56:51 EST 2003
> However, I noticed that
>
> >>> isinstance(object.__new__,staticmethod)
> False
>
> Therefore if I try to retrieve all the staticmethods from a class I will
> miss __new__. That's unfortunate, even if I understand this is done in
order
> to avoid to write
>
> __new__=staticmethod(__new__)
>
> each time I redefine __new__. In a sense, __new__ is an "implicit" static
> method.
no, object.__new__ is a special case:
>>> class C(object):
... def __new__(cls):
... print "C__new__"
... return object.__new__(cls)
...
>>> c=C()
C__new__
>>> C.__new__
<function __new__ at 0x00795F40>
>>> C.__dict__['__new__']
<staticmethod object at 0x00792010>
>>> isinstance(_,staticmethod)
1
>>> isinstance(object.__dict__['__new__'],staticmethod)
0
for normal new style classes, __new__ lives as staticmethod in the class
__dict__ but notice it is unwrapped to a function on lookup.
That's true for general staticmethods:
>>> class C(object):
... def f():
... return 'f'
... f=staticmethod(f)
...
>>> C.f
<function f at 0x00794220>
>>> C.__dict__['f']
<staticmethod object at 0x007941F0>
regards.
More information about the Python-list
mailing list