accessing superclass methods from subclass

ben thomasstruth at gmail.com
Sat May 8 23:24:28 EDT 2010


Ok, thanks for the info.

What would be a better way to do this?  What I'm trying to do is treat
things in a reasonable OOP manner (all fairly new to me, esp. in
Python).  Here's a made-up example with a little more context.  Let's
say you're making a drawing program that can draw various shapes.  So
in the interest of not repeating oneself, I want a class Shape that
handles everything that shapes have, such as a color, and a location.
Then I can subclass Shape to create Square, which has code specific to
drawing a square (e.g. 4 equal sides).  So, like this:

class Shape:

    x = 0
    y = 0

    def setColor(self,color):
        self.color = color

    def setLocation(self,x,y):
        self.x = x
        self.y = y

    def getLocation(self):
        return [self.x,self.y]

class Square(Shape):

    size = 0

    def __init__(self,size):
        self.size = size

    def draw(self):
        location = getLocation()
        # code to draw shape from location[0],location[1] at size size
        # etc...

It seems to me that you would want the location code handled in the
Shape class so that I'm not rewriting it for Circle, Triangle, etc.,
but I'm not allowed to call any of those methods from the subclass.  I
must be thinking of this in the wrong way.  Help?

thanks!



On May 8, 7:05 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Sat, May 8, 2010 at 4:50 PM, ben <thomasstr... at gmail.com> wrote:
> > Why doesn't this work:
>
> > class C1:
> >    def f1(self):
> >        print("f1")
>
> > class C2(C1):
> >    f1()
>
> > It throws this error:
>
> > Traceback (most recent call last):
> >  File "./c1.py", line 7, in <module>
> >    class C2(C1):
> >  File "./c1.py", line 8, in C2
> >    f1()
> > NameError: name 'f1' is not defined
>
> > f1() is an attribute of class C1, C2 inherits C1, so why can't it see
> > it?
>
> The way classes work in Python, C2 isn't actually created until after
> its body suite has been executed, so that's why Python can't find f1.
>
> Additionally, it makes no sense to call an *instance* method such as
> f1() in a class context. Or in Java-speak: you can't call a non-static
> method in a static context.
>
> Cheers,
> Chris
> --http://blog.rebertia.com




More information about the Python-list mailing list