[h2o]
May 11th, 2006, 10:31 AM
I am new to python and ran into something which I found really strange: dynamic binding does not seem to work at all?
If I have the following to classes (with unneccessary functions removed):
class Person:
"""Defines a general person """
def __description(self):
return "Person function"
def printInfo(self):
print self.__description()
class Parent(Person):
"""Defines a parent """
def __description(self):
return "Parent function"
But when I run
a = Person()
b = Parent()
a.printInfo()
b.printInfo()
both calls return "Person function" instead of "Person function" and "Parent function"
In java, which I am more familiar with, the rte checks what type of object the instance is and then executes the correct function (Parent.__description for a Parent-object and Person.__description for a Person object) but python seems to not do this. Actually, if I change my "printInfo" function to "print self.__description" it clearly shows that Person-objects return "<bound method Person.__description of ..." while Parent-objects return "<bound method Parent.__description of ..." so I really can't understand why it won't work!
What am I missing?
If I have the following to classes (with unneccessary functions removed):
class Person:
"""Defines a general person """
def __description(self):
return "Person function"
def printInfo(self):
print self.__description()
class Parent(Person):
"""Defines a parent """
def __description(self):
return "Parent function"
But when I run
a = Person()
b = Parent()
a.printInfo()
b.printInfo()
both calls return "Person function" instead of "Person function" and "Parent function"
In java, which I am more familiar with, the rte checks what type of object the instance is and then executes the correct function (Parent.__description for a Parent-object and Person.__description for a Person object) but python seems to not do this. Actually, if I change my "printInfo" function to "print self.__description" it clearly shows that Person-objects return "<bound method Person.__description of ..." while Parent-objects return "<bound method Parent.__description of ..." so I really can't understand why it won't work!
What am I missing?