oo problem

James Stroud jstroud at mbi.ucla.edu
Sun Dec 10 16:24:19 EST 2006


Tool69 wrote:
> Thanks James,
> 
> It was a matter of choice : in my earlier version, all primitives know
> how to draw themselves, but they needed to have a parent and I found it
> was not good for aesthetic reasons.
> 
> but I think I've been misunderstood. Let's take a concrete sample :
> 
> I've got a Paper instance(-5,5,5,5) (the coordinates of lower-left and
> upper-right rectangle: xlowleft, ylowlef, xupright, yupright) of my
> paper.
> I want now to draw an x-axis on it with some ticks and maybe lables, I
> need to catch the xlowleft and xupright variables of the Paper inside
> my primitive, ok ?
> The problem lies inside my primitive, not on the draw method.
> Here's a simplified example, I need to retrieve paper.xll and
> paper.xur:
> 
> class Graduate_x( primitive ):
>     def __init__(self, orient = 'se', xorigin = 0, yorigin=0, xunit=
> 1.0, yunit=1.0):
>          primitive.__init__(self)
>          for k in range( int(math.floor(( paper.xll - xorigin)/xun)),
> int(math.floor((paper.xur-xorigin)/yun)) ):
>              ...something to construct my path
> 

You can simply pass the paper to the __init__ method and even store a 
reference to the paper there if you have to re-construct the path later, 
say if the paper size changes or the origins of the primitive change. I 
wouldn't advise storing references to the primitives in the paper as 
well, because you would get circular references.

Global variables are probably not the way to handle this. For OO, if you 
feel like you need a global variable, try to think up an object that can 
sensibly contain all the objects that would be global.

James



More information about the Python-list mailing list