instance attributes not inherited?

John M. Gabriele john_sips_teaz at yahooz.com
Mon Jan 16 02:50:59 CET 2006


John M. Gabriele wrote:
> David Hirschfield wrote:
> 
>> Nothing's wrong with python's oop inheritance, you just need to know 
>> that the parent class' __init__ is not automatically called from a 
>> subclass' __init__. Just change your code to do that step, and you'll 
>> be fine:
>>
>> class Parent( object ):
>>     def __init__( self ):
>>         self.x = 9
>>
>>
>> class Child( Parent ):
>>     def __init__( self ):
>>     super(Child,self).__init__()
>>        print "Inside Child.__init__()"
>>
>> -David
>>
> 
> How does it help that Parent.__init__ gets called? That call simply
> would create a temporary Parent object, right? I don't see how it
> should help (even though it *does* indeed work).

Sorry -- that question I wrote looks a little incomplete: what I meant
to ask was, how does it help this code to work:

---- code ----
#!/usr/bin/python

class Parent( object ):
     def __init__( self ):
         self.x = 9
         print "Inside Parent.__init__()"

     def wash_dishes( self ):
         print "Inside Parent.wash_dishes(), washing", self.x, "dishes."


class Child( Parent ):
     def __init__( self ):
         super( Child, self ).__init__()
         print "Inside Child.__init__()"


c = Child()
c.wash_dishes()
---- /code ----

since the x instance attribute created during the
super( Child, self ).__init__() call is just part of what looks to be
a temporary Parent instance.




-- 
(remove zeez if demunging email address)



More information about the Python-list mailing list