[Tutor] Calling super classs __init__?
Andreas Kostyrka
andreas at kostyrka.org
Sun Mar 23 21:20:04 CET 2008
Am Mittwoch, den 19.03.2008, 10:36 -0300 schrieb Ricardo Aráoz:
> Luciano Ramalho wrote:
> > Nowadays the best practice for invoking a method from all superclasses
> > (yes, multiple inheritance) is this:
> >
> > class SubClass(BaseClass):
> > def __init__(self, t, *args, **kw):
> > super(SubClass, self).__init__(*args, **kw)
> > # do something with t
> >
> > That way you let Python decide which superclasses your SubClass has,
> > instead of hard-coding it in several places.
> >
>
> You are actually hard-coding it here too, "class SubClass(BaseClass):"
> has "BaseClass" hard-coded. All you do here is hard-code it once instead
> of twice.
Yes and no. Yes a human has specified the relationships. But you do not
have to specify what other baseclasses your class has. This is relevant
for multiple inheritence, like:
D => B => A
D => C => A
The problem is mostly hiding implementation details.
Using super, you just let Python pick the "next" class to give control.
Using B.__init__ and C.__init__, A.__init__ would be called twice.
Andreas
>
> > Cheers,
> >
> > Luciano
> >
> >
> > On Tue, Mar 18, 2008 at 9:37 PM, John Fouhy <john at fouhy.net> wrote:
> >> On 19/03/2008, Allen Fowler <allen.fowler at yahoo.com> wrote:
> >> > I have a super class that accepts many arguments to it's constructor, and a subclass that should define one additional argument.
> >> >
> >> > What's the most "pythonic" way to make this work?
> >>
> >> class BaseClass(object):
> >> def __init__(self, x, y, z, foo='foo'): # whatever
> >> # etc
> >>
> >> class SubClass(BaseClass):
> >> def __init__(self, t, *args, **kw):
> >> BaseClass.__init__(self, *args, **kw)
> >> # do something with t
> >>
> >> This does mean that the special sub class argument has to come before
> >> the base class arguments when you create instances.
> >>
> >> Whether you call BaseClass.__init__ early or late in the subclass init
> >> method could depend on what your classes are doing. Remember, in
> >> Python, __init__ only initializes objects, it doesn't create them.
> >> It's just another bit of code that you can call whenever you want.
> >>
> >> --
> >> John.
> >>
> >>
> >> _______________________________________________
> >> Tutor maillist - Tutor at python.org
> >> http://mail.python.org/mailman/listinfo/tutor
> >>
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
> _______________________________________________
> 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/20080323/75d5d979/attachment.pgp
More information about the Tutor
mailing list