[Tutor] Calling super classs __init__?
Andreas Kostyrka
andreas at kostyrka.org
Fri Mar 21 10:25:36 CET 2008
It does not return them at once.
It returns them piece by piece:
andreas at andi-lap:/tmp> cat mro.py
class A(object):
v=lambda self: "A"
class B(object):
v=lambda self: "B"
class C(B,A):
v=lambda self: "C"
print C.__mro__
c=C()
print c.v()
print super(C, c).v()
print super(B, c).v()
andreas at andi-lap:/tmp> python2.5 mro.py
(<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <type 'object'>)
C
B
A
Andreas
Am Freitag, den 21.03.2008, 23:11 -0500 schrieb tiger12506:
> Woah. Either you're leaving out essential info, or python got a lot more
> complicated.
>
> Firstly, super returns all base classes. How? Does it return a tuple of
> them, or a container object, or is this something horribly worse such as
> syntactic sugar?
>
> It doesn't make sense for it to return a tuple, then super().__init__ would
> call the init method of the tuple, not each class, so then syntactic sugar
> would have to be involved. I doubt that..
>
> A container object could use __getattr__ to magically call each of the
> __init__ methods of the contained class references.
> The most logical is the container object. But that leaves things open for
> duplicate calls to __init__
> So the container would have to keep track of the base classes it has already
> called... which might be the ordered list __mro__, but those are class
> specific, not global, so each super class would not know if an __init__ has
> been called or not... But then again, you would need duplicate calls because
> each level of super would need different initiation.
>
> I apologize. Would you care to explain this a little more technically?
> Specific and accurate are better than translated into layman's terms. I've
> been with python for a while now. thanks.
>
> >Actually, super returns all base classes, all in it's own time.
> >
> >Basically, every class has a member __mro__, that contains a
> >consistently ordered list of classes.
> >
> >super needs the class from where it is being called to locate the right
> >place in the __mro__ and to hand you a wrapper around self for the next
> >base class in this list.
> >
> >This way, if all classes use super, they can cooperativly call all
> >implementations of a given method.
> >
> >That's the theory. In practice there are a number of pitfalls which
> >makes super problematic. ;)
> >
> >Andreas
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20080321/38fe9cad/attachment.pgp
More information about the Tutor
mailing list