oo problem

Kay Schluehr kay.schluehr at gmx.net
Sun Dec 10 05:56:55 EST 2006


Tool69 schrieb:

> Hi,
> I've got a simple but difficult problem :
>
> Suppose I've got a Paper class, on wich I can draw i.e a rectangle, a
> circle or whatever.
>
> class Paper(...):
>     def __init__(self, paperx, papery):
>         self.paperx = paperx
>         self.papery = papery
>         ....
>     def draw(self, Primitive_Object):
>         ....
> class Rectangle(  ): <--- a Primitive_Object
>     ...
>
> Now, inside my Rectangle class, I need to recover the Paper instance
> who called it (because I need the paper sizes to calculate something).

You can simply pass a paper reference to a rectangle object. But better
you pass only the information to the rectangle that is necessary,
because you decouple the objects this way:

class Rectangle:
    def __init__(self):
         self.paper_coordinates = [0,0]  # default value

class Paper:
     ...
    def draw(self, shape):
         shape.paper_coordinates = self.x, self.y
         shape.draw()
         ...




More information about the Python-list mailing list