Rui Maciel wrote:

> Let:
> - class Point be a data type which is used to define points in space
> - class Line be a data type which possesses an aggregate relationship with
> objects of type Point
> - class Model be a container class which stores collections of Point and
> Line objects
> 
> 
> 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.
> 
> To reflect this class relationship, I've defined the following classes:
> 
> <code>
> class Point:

Don't add 

>         position = []

to your code. That's not a declaration, but a class attribute and in the 
long run it will cause nothing but trouble.

>         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 = []
> 
> 
> </code>
> 
> 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.
> 
> What's the Python way of achieving the same effect?

Your code already does that. Here's a minimally cleaned up version:

$ cat tmp_points.py
class Point:
    def __init__(self, x, y, z=0):
        self.position = (x, y, z)
    def __str__(self):
        return "%s" % (self.position,)

class Line:
    def __init__(self, p_i, p_f):
        self.points = (p_i, p_f)
    def __str__(self):
        return "%s-->%s" % self.points

a = Point(1, 2, 3)
b = Point(4, 5, 6)
ab = Line(a, b)
print(ab)
a.position = (10, 20, 30)
print(ab)

$ python3 tmp_points.py 
(1, 2, 3)-->(4, 5, 6)
(10, 20, 30)-->(4, 5, 6)


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

Reply via email to