Newbie: question regarding references and class relationships

Roy Smith roy at panix.com
Mon Jun 10 09:35:43 EDT 2013


In article <kp4jf4$5fu$1 at dont-email.me>,
 Rui Maciel <rui.maciel at gmail.com> wrote:
 
> Essentially, a Model object stores lists of Point objects and Line objects, 
> and Line objects include references to Point objects which represent the 
> starting and ending point of a line.
>
> class Point:
>         position = []
> 
>         def __init__(self, x, y, z = 0):
>                 self.position = [x, y, z]
> 
> class Line:
>         points = ()
>         def __init__(self, p_i, p_f):
>                 self.points = (p_i, p_f)
> 
> class Model:
>         points = []
>         lines = []
> 
>
> It would be nice if, whenever a Point object was updated, the Line objects 
> which are associated with it could reflect those updates directly in 
> Line.p_i and Line.p_f.

Have you tried running the code you wrote?  It does that already!  When 
you do something like:

my_list = [obj1, obj2]

in Python, the objects are stored by reference (not just lists, all 
assignments are by reference).



More information about the Python-list mailing list