changing a var by reference of a list

Diez B. Roggisch deets at nospam.web.de
Tue May 8 09:34:28 EDT 2007


Jorgen Bodde wrote:

> Hi,
> 
> I am trying to simplify my code, and want to automate the assigning of
> variables I get back from a set. I was thinking of putting the
> variables I want changed in a list:
> 
> L = [self._varA, self._varB ]
> 
> self._varA is a variable I want to change when I pass L to a function.
> I know doing this;
> 
> L[0] = 12
> 
> Will replace the entry self._varA with 12, but is there a way to
> indirectly change the value of self._varA, through the list, something
> like a by-reference in C++ or a pointer-pointer?

No, there isn't.

But you could do

L = ['_varA']

for v in L:
    setattr(self, v, value)

Diez



More information about the Python-list mailing list