J
John M. Gabriele
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
----------------------- 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