Question about inheritance...
Mike Meyer
mwm at mired.org
Sat Oct 22 17:54:44 EDT 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
> Shape:
>
> My quesiton is can a method of the Shape class call a method in Circle,
> or Square etc...?
Yup:
>>> class Shape(object):
... def commented_draw(self):
... print "Drawing", self.__class__.__name__
... self.draw()
...
>>> class Circle(Shape):
... def draw(self):
... print "Drawing a Circle"
...
>>> c = Circle()
>>> c.commented_draw()
Drawing 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
--
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
mailing list