[Tutor] Re: Class - superclass

Bernard Lebel 3dbernard at gmail.com
Mon Apr 11 15:11:28 CEST 2005


Thanks a lot, now it's clear.


Bernard


On Apr 8, 2005 3:48 PM, Andrei <project5 at redrival.net> wrote:
> Bernard Lebel wrote on Fri, 8 Apr 2005 15:05:13 -0400:
> 
> > I'm experimenting with basic inheritance concepts, and something that
> > I would assume to work returns an error.
> >
> >>>> class A:
> > ...     def __init__( self ):
> > ...             self.a = 13
> > ...
> >>>> class B( A ): # create sub-class of class A
> > ...     def __init__( self ):
> > ...             self.b = 14
> 
> Call the __init__ of the ancestor explicitly:
> 
> >>> class B(A):
> ...     def __init__(self):
> ...         A.__init__(self)
> ...         self.b = 14
> >>> b = B()
> >>> b.a, b.b
> (13, 14)
> 
> B inherits everything from A, but by defining B.__init__, the __init__
> inherited from A is replaced, so you'll need to call it explicitly. Python
> has no way of knowing that you still want to use the original __init__ too
> unless you tell it so. To demonstrate the fact that __init__ is indeed
> inherited:
> 
> >>> class C(A):
> ...     pass
> >>> c = C() # inherited __init__ (A.__init__) is called
> >>> c.a
> 13
> 
> --
> Yours,
> 
> Andrei
> 
> =====
> Real contact info (decode with rot13):
> cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
> gur yvfg, fb gurer'f ab arrq gb PP.
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list