Reputation: 1
I work in Python 2.4 (comes with the system).
I try to compile a list of objects. Each Object has an attribute that is a list of other objects. Whatever I do it seems to me that the attribute(list) stores only a reference and not the list itself. ControlPoint.LeafPairList
is a reference to LeafList
but does not contain the List itself.
Later in the script I try to address the LeafPair with ControlPoint.LeafPairList[i] that gives me always the same settings
Any suggestions to make it work? Thanks in advance.
Example:
def ReadControlPoint(fh,ControlPoint):
line = fh.readline()
while not line.startswith('}'):
Scratch = line.split(' = ' )
if Scratch[0] == 'LeafPairList':
ReadLeafPairList(fh, LeafList)
setattr(ControlPoint, 'LeafPairList', LeafList)
else:
setattr(ControlPoint, Scratch[0], Scratch[1].strip('\n"'))
line = fh.readline()
def ReadLeafPairList(fh, LeafList):
del LeafList[:]
line = fh.readline()
while not line.startswith('}'):
Scratch = line.split(' = ')
Scratch = Scratch[1].strip('"\n').split()
Leafs = LeafPair(Scratch)
LeafList.append(Leafs)
line = fh.readline()
The List looks something like that:
Machine = "Infinity_1"
Gantry = " 310.0"
Collimator = " 0.0"
Couch = " 0.0"
Weight = " 29.46 %"
NumberOfControlPoints = " 7"
NumberOfLeafPairs = " 80"
LeavesCanOverlap = " 1"
X2_Value = " 4.5"
X1_Value = " 4.5"
Y1_Value = " 9.0"
Y2_Value = " 9.0"
ControlPointList = {
ControlPoint = {
ControlPoint = " 0"
Weight = " 0.3"
LeftJawPosition = " 4.5"
RightJawPosition = " 4.5"
TopJawPosition = " 9.0"
BottomJawPosition = " 9.0"
LeafPairList = {
LeafPair(0) = " 0.5 0.0 0.5 -19.8"
LeafPair(0) = " 0.5 0.0 0.5 -19.2"
LeafPair(0) = " 0.5 0.0 0.5 -18.8"
LeafPair(0) = " 0.5 0.0 0.5 -18.2"
LeafPair(0) = " 0.5 0.0 0.5 -17.8"
}
}
ControlPoint = {
ControlPoint = " 1"
Weight = " 0.3"
LeftJawPosition = " 4.5"
RightJawPosition = " 4.5"
TopJawPosition = " 9.0"
BottomJawPosition = " 9.0"
LeafPairList = {
LeafPair(1) = " 0.5 0.0 0.5 -19.8"
LeafPair(1) = " 0.5 0.0 0.5 -19.2"
...
}
}
}
Upvotes: 0
Views: 88
Reputation: 14360
Ok thats because in Python all are references. I'll explain with a simple example:
>>> l = [0, 0, 0]
>>> a = l
>>> a[0] = "Changed"
>>> print (l)
["Changed", 0, 0]
>>> print (a)
["Changed", 0, 0]
This happend because with the statement a=l
we have just put another name to the object [0, 0, 0]
To achive what you want you can use copy
module
>>> import copy
>>> l = [0, 0, 0]
>>> a = copy.copy(l) # This is the only change.
>>> a[0] = "Changed"
>>> print (l)
[0, 0, 0]
>>> print (a)
["Changed", 0, 0]
Upvotes: 1