Question about inheritance...

K

KraftDiner

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...?
 
O

Oliver Andrich

Hi,

22 Oct 2005 14:40:09 -0700 said:
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
 
M

Mike Meyer

KraftDiner said:
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:
.... def commented_draw(self):
.... print "Drawing", self.__class__.__name__
.... self.draw()
.... .... def draw(self):
.... print "Drawing a Circle"
.... 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
 
R

Ron Adam

KraftDiner said:
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
 
K

KraftDiner

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__
 
K

KraftDiner

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..
 
A

Alex Martelli

KraftDiner said:
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
 
R

Ron Adam

KraftDiner said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,349
Members
48,028
Latest member
Rigor4

Latest Threads

Top