Newbie: question regarding references and class relationships
Rui Maciel
rui.maciel at gmail.com
Thu Jun 13 08:06:26 EDT 2013
Rick Johnson wrote:
> On Monday, June 10, 2013 8:18:52 AM UTC-5, Rui Maciel wrote:
>> [...]
>>
>> <code>
>> class Point:
>> position = []
>> def __init__(self, x, y, z = 0):
>> self.position = [x, y, z]
>
> Firstly. Why would you define a Point object that holds it's x,y,z values
> in a list attribute? Why not store them as self.x, self.y and self.z?
<snip/>
The position in space is represented as a vector, which is then used in a
series of operations.
Currently I'm using numpy arrays to represent vectors.
> Secondly, why would store the position of the Point as a class attribute?
<snip/>
I've answered this 3 days ago. I'm still learning Python, and python.org's
tutorial on classes didn't explicitly covered the differences between class
and instance attributes.
> If you construct your code properly this can be achieved. If each point is
> an object, and lines are merely holding references to two point objects
> that define the start and end position of an imaginary "line", then
> updates on the points will be reflected in the Line object.
This was already covered three days ago in another post in this thread.
I'll quote the post below
<quote>
I've tested the following:
<code>
model = Model()
model.points.append(Point(1,2))
model.points.append(Point(1,4))
line = Line( model.points[0], model.points[1])
# Case A: this works
model.points[0].position = [2,3,4]
line.points
# Case B: this doesn't work
test.model.points[0] = test.Point(5,4,7)
line.points
</code>
Is there a Python way of getting the same effect with Case B?
</quote>
Rui Maciel
More information about the Python-list
mailing list