OO issues in python
Michael T. Babcock
mbabcock at fibrespeed.net
Wed Dec 17 16:41:15 EST 2003
> in python, when a class, say Father, has a member that itself is an
> instance of another class, say Child, an instance of Father actually
> only has a reference to a Child, not the Child object itself, right?
I've seen a few responses to this but I wanted to point something out
... an instance of the Father will not know about the Child at all.
class Father:
pass
class Child(Father):
pass
f = Father()
# f knows nothing about Child
However, the rest of your message sounds like "how do I do virtual
functions?" to me ... you're aware that you can replace parent
functions, correct?
class Father:
def run(self):
'''run the function; used in children'''
pass
def __init__(self):
'''this is stupid but its just an example'''
self.run()
class Child(Father):
def run(self):
'''child version of the run function'''
print "Hello"
>>> c = Child()
Hello
--
Michael T. Babcock
C.T.O., FibreSpeed Ltd.
http://www.fibrespeed.net/~mbabcock
More information about the Python-list
mailing list