Can __iter__ be used as a classmethod?

Samuele Pedroni pedronis at bluewin.ch
Thu Mar 6 17:30:30 EST 2003


"Greg Chapman" <glc at well.com> ha scritto nel messaggio
news:90oe6vccthidi7h443od1ndrls8h5oakr5 at 4ax.com...
> On Tue, 4 Mar 2003 23:21:16 +0100, "Samuele Pedroni" <pedronis at bluewin.ch>
> wrote:
>
> >It is worth to notice that once you override one of them (like g in D
> >overriding g in C) it is impossible to call in a clean way the parent
> >version (C g) passing the derived class (D), which is what would happen
in
> >case of no overriding (if D would not define g, then D.g() would call g
in C
> >passing D), (so above C.g() obviously is calling g in C passing C).
"Let's
> >call this their price".
> >
>
> This works for me with 2.22 and 2.3a2:
>
> class C(object):
>     def g(cls):
>         print 'C', cls
>     g = classmethod(g)
>
> class D(C):
>     def g(cls):
>         print 'D', cls
>         super(D, cls).g()
>     g = classmethod(g)
>
> Both D.g()  and D().g() print:
>
> D <class '__main__.D'>
> C <class '__main__.D'>
>
> Am I missing some subtlety here?

no, good catch.

Although it is an underdocumented feature, e.g. the pure python version of
super here

http://www.python.org/2.2.2/descrintro.html#cooperation

would not work. Here's how the feature was added:

www.python.org/sf/535444

and for a normal method:

>>> class C(object):
...  def f(self): pass
...
>>> class D(C): pass
...
>>> D.f
<unbound method D.f>
>>> super(D,D).f
<bound method D.f of <class '__main__.D'>>

super(D,D) appear to misbehave.






More information about the Python-list mailing list