1

I thought my question should've been pretty common but I couldn't find the answer online. I have a function that takes in 2 arguments and has 2 outputs. I want to run a list of these 2 arguments through the function and assign the output to the element in the list. Let me explain, the 2 arguments are in these 2 lists:

Lengths = [self.xS, self.yS, self.zS, self.Len1, self.Len2, self.Thick]
LengthsUnit = [self.xSUnit, self.ySUnit, self.zSUnit, self.Len1Unit, self.Len2Unit, self.ThickUnit]

The for loop I have constructed to run these arguments through the function looks like this:

for length, unit in zip(Lengths, LengthsUnit):
    length, unit = ConvertToSIVarUnit(self, length, unit, 'meter', 'Length')

After completion, I want the things in the list (take self.xS for example) to be modified. So the output of the first print statement should be different from the last:

Lengths = [self.xS, self.yS, self.zS, self.Len1, self.Len2, self.Thick]
LengthsUnit = [self.xSUnit, self.ySUnit, self.zSUnit, self.Len1Unit, self.Len2Unit, self.ThickUnit]

print(self.Xs)

for length, unit in zip(Lengths, LengthsUnit):
    length, unit = ConvertToSIVarUnit(self, length, unit, 'meter', 'Length')

print(self.xS)

But the output of the 2 print functions are the same so the for-loop-function combination might not be working.

1 Answer 1

2

You need to assign to the list elements, not the variables that hold the list values. Use enumerate() to get the indexes.

for i, (length, unit) in enumerate(zip(Lengths, LengthsUnit)):
    Lengths[i], LengthsUnit[i] = ConvertToSIVarUnit(self, length, unit, 'meter', 'Length')

Things would probably be easier if you didn't keep the lengths and units in different lists, though. Use a list of dictionaries or tuples to keep the values and units together.

It's not possible for changing the list to affect the original object attributes. You could use a list of attribute names rather than values, then use getattr() and setattr.

Lengths = ['xS', 'yS', 'zS', 'Len1', 'Len2', 'Thick']
LengthsUnit = ['xSUnit', 'ySUnit', 'zSUnit', 'Len1Unit', 'Len2Unit', 'ThickUnit']
for length, unit in zip(Lengths, LengthsUnit):
    new_length, new_unit = ConvertToSIVarUnit(self, getattr(self, length), getattr(self, unit), 'meter', 'Length')
    setattr(self, length, new_length)
    setattr(self, unit, new_unit)

print(self.xS)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for answering. I still want to reference say for example self.xS instead of Lengths[0] every time I want the use self.xS. Is there a way to do this inside the loop or should I look for try something else?
Once you put a value inside a list, it's no longer related to the original location that it came from.
Python doesn't have anything like C pointers that can refer to attributes of other objects.
I've added code that puts the attribute names in the lists to allow indirecting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.