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

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to