accessing superclass methods from subclass

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed May 12 03:25:23 EDT 2010


Chris Rebert a écrit :
(snip)
> Here is how I would rewrite your example:
> 
> class Shape(object):
>     def __init__(self, x=0, y=0):
>         self.x = x
>         self.y = y
> 
>     @property
>     def location(self):
>         return (self.x, self.y)
> 
>     @location.setter
>     def location(self, val):
>        self.x, self.y = val

Not necessary but helps documenting the expected interface:

       def draw(self):
           raise NotImplementedError("Abstract method")

> class Square(Shape):
>    def __init__(self,size):
>        super(Square, self).__init__()
>        self.size = size

Why can't I specify a Square location ??? This looks rather inconsistant 
and inconveniant to me...

     def __init__(self, x=0, y=0, size=0):
         super(Square, self).__init__(x, y)
         self.size = size
	

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



More information about the Python-list mailing list