By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

Question about inheritance...

P: n/a
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...?

Oct 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi,

22 Oct 2005 14:40:09 -0700, KraftDiner <bo*******@yahoo.com>:
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...?


even there would exist a way to accomplish that, I would suggest to
rethink your class hierachy. Such requirements can show serious design
problems.

Best regards,
Oliver

--
Oliver Andrich <ol************@gmail.com> --- http://roughbook.de/
Oct 22 '05 #2

P: n/a
"KraftDiner" <bo*******@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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 22 '05 #3

P: n/a
KraftDiner wrote:
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...?

This looks familiar. :-)

Yes, it can if it has references to them.

Could you explain a little better what you are doing.

Since I'm working on the same (or similar) thing maybe we can share our
results, (or efforts).

Cheers,
Ron
Oct 22 '05 #4

P: n/a
Well here is a rough sketch of my code...
This is giving my two problems.

1) TypeError: super() argument 1 must be type, not classobj
2) I want to be sure the the draw code calls the inherited classes
outline and not its own...

class Shape:
def __init__(self):
pass
def render(self):
print self.__class___
self.outline()
def outline(self):
pass

class Rect(Shape):
def __init__(self):
super(self.__class__, self).__init__()
def render(self):
super(self.__class__, self).draw()
def outline(self):
print 'outline' + self.__class__

Oct 23 '05 #5

P: n/a
This is what I've got so far:
class Shape(object):
def __init__(self):
pass
def render(self):
print 'Shape render'
self.outline()
def outline(self):
pass

class Rect(Shape):
def __init__(self):
super(self.__class__, self).__init__()
def render(self):
super(self.__class__, self).render()
def outline(self):
print 'Rect outline'

r = Rect()
r.render()

The output:

Shape render
Rect outline

Cool.. I guess its working..

Oct 23 '05 #6

P: n/a
KraftDiner <bo*******@yahoo.com> wrote:
Well here is a rough sketch of my code...
This is giving my two problems.

1) TypeError: super() argument 1 must be type, not classobj
Make your classes new-style (have Shape inherit from object) to fix
this. You're using the legacy (old-style) object model (which remains
for backwards compatibility only).
2) I want to be sure the the draw code calls the inherited classes
outline and not its own...
Call anything on self, and you'll use the inherited class.

class Shape:
change to: class Shape(object):
def __init__(self):
pass
remove this method, no need for it.
def render(self):
print self.__class___
Use two trailing underscores, NOT three.
self.outline()
def outline(self):
pass
Use as the body "raise NotImplementedError" to make sure that
Shape.outline never gets accidentally called.

class Rect(Shape):
def __init__(self):
super(self.__class__, self).__init__()
def render(self):
super(self.__class__, self).draw()
You never defined a method named 'draw', do you mean 'render'?
def outline(self):
print 'outline' + self.__class__


Alex
Oct 23 '05 #7

P: n/a
KraftDiner wrote:
This is what I've got so far:
class Rect(Shape):
def __init__(self):
super(self.__class__, self).__init__()
Should be
super(Rect, self).__init__()
def render(self):
super(self.__class__, self).render()


ditto

In this example it doesn't make any difference but with a deeper inheritance hierachy it does. See
http://www.python.org/2.2.3/descrintro.html#cooperation

Kent
Oct 23 '05 #8

P: n/a
KraftDiner wrote:
Well here is a rough sketch of my code...
This is giving my two problems.

1) TypeError: super() argument 1 must be type, not classobj
2) I want to be sure the the draw code calls the inherited classes
outline and not its own...

class Shape:
def __init__(self):
pass
def render(self):
print self.__class___
self.outline()
def outline(self):
pass

class Rect(Shape):
def __init__(self):
super(self.__class__, self).__init__()
def render(self):
super(self.__class__, self).draw()
def outline(self):
print 'outline' + self.__class__


I think Alex probably answered your question. I'm still trying to
figure out how use inheritance effectively myself.
Are you using this as an way to learn Python or do you want to use
Python to render images?
What I'm trying to do in relation to this is to build a "small" subset
of the SVG standard in tkinter. It looks like the SVG standard is going
to be very popular graphic format on the web, hand held devices, and as
a general graphics format.

http://www.w3.org/TR/SVG/

Python doesn't have good SVG support built in without importing a third
party library, and the ones that are available need other libraries,
etc... They don't really support using SVG directly in programs. So
I'm experimenting with emplimenting some of the SVG functionality in
Tkinter for drawing icons in dialog box's. It's an experiment at the
moment, but I'd like to see it grow into something more.

It doesn't need to be exactly like it, but the closer it is to the way
the SVG standard works, the better. That would make it easier to use
some 'simple' existing SVG images, and easier to create SVG files as
well, as it closes the gap between the canvas object and the VGA standard.

Anyway, I'm more than willing to get involved in a group to do this if
anyone is interested and also thinks it may be worth while.

Cheers,
Ron



Oct 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.