Question about inheritance...
mwm at mired.org
Sat Oct 22 23:54:44 CEST 2005
"KraftDiner" <bobrien18 at yahoo.com> writes:
> I have a base class called Shape
> And then classes like Circle, Square, Triangle etc, that inherit from
> My quesiton is can a method of the Shape class call a method in Circle,
> or Square etc...?
>>> class Shape(object):
... def commented_draw(self):
... print "Drawing", self.__class__.__name__
>>> class Circle(Shape):
... def draw(self):
... print "Drawing a Circle"
>>> c = Circle()
Drawing a Circle
Or maybe you meant invoking them directly, which a method in Shape
would do by calling Circle.draw(self). The latter is ugly - you should
use self.draw() to invoke the draw routine that's correct for self.
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list