instance attributes not inherited?



The following short program fails:


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

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


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


p1 = Parent() p2 = Parent() c1 = Child() foo = [p1,p2,c1]

for i in foo:
    print "x =", i.x
----------------- /code ----------------------



yielding the following output:

---------------- output ------------------
Inside Parent.__init__()
Inside Parent.__init__()
Inside Child.__init__()
x = 9
x = 9
x =
Traceback (most recent call last):
  File "./foo.py", line 21, in ?
    print "x =", i.x
AttributeError: 'Child' object has no attribute 'x'
---------------- /output ---------------------


Why isn't the instance attribute x getting inherited?

My experience with OOP has been with C++ and (more
recently) Java. If I create an instance of a Child object,
I expect it to *be* a Parent object (just as, if I subclass
a Car class to create a VW class, I expect all VW's to *be*
Cars).

That is to say, if there's something a Parent can do, shouldn't
the Child be able to do it too? Consider a similar program:

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


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

    def wash_dishes( self ):
        print "Just washed", self.x, "dishes."


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


p1 = Parent() p2 = Parent() c1 = Child() foo = [p1,p2,c1]

for i in foo:
    i.wash_dishes()
------------------- /code -----------------------

But that fails with:

------------------- output ----------------------
Inside Parent.__init__()
Inside Parent.__init__()
Inside Child.__init__()
Just washed 9 dishes.
Just washed 9 dishes.
Just washed
Traceback (most recent call last):
  File "./foo.py", line 24, in ?
    i.wash_dishes()
  File "./foo.py", line 10, in wash_dishes
    print "Just washed", self.x, "dishes."
AttributeError: 'Child' object has no attribute 'x'
------------------- /output ---------------------

Why isn't this inherited method call working right?
Is this a problem with Python's notion of how OO works?

Thanks,
---J

--
(remove zeez if demunging email address)
.



Relevant Pages

  • Re: A superclass using a child classes methods
    ... I'd like the parent class to have an encode function ... that uses each of the child classes' packing methods. ... def foo: ...
    (comp.lang.python)
  • Re: instance attributes not inherited?
    ... 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__. ... def __init__: super.__init__ ... 'Child' object has no attribute 'x' ...
    (comp.lang.python)
  • Re: OOP: method overriding works in mysterious ways?
    ... > class Parent(Grand_parent): ... > def speak(self): ... > def advise: ... > class Child(Parent): ...
    (comp.lang.python)
  • Re: Setting class variable from parent class?
    ... I'm trying to teach my Parent class how to set variables into its Child ... def self.set_my_class_variable ...
    (comp.lang.ruby)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)