Python: Assigning "through" an iterator
You could use a list comprehension:
foo[:] = [expression for bar in foo]
or, if the assignment is too complicated for an expression, you could use a function, func
:
foo[:] = [func(bar) for bar in foo]
If you only have an iterator, then there is no way to reassign values to the underlying container for there is no guarantee that there even is an underlying container:
def generator():
for i in range(10):
yield i
for bar in generator():
# Nothing you put here can change what's going on in generator
From my understanding, your use case is something like this:
class Z:
def __init__(self):
self.a, self.b, self.c = 1, 2, 3
def it(self):
for x in self.a, self.b, self.c:
yield x
z = Z()
for x in z.it():
if x == 1:
x = 42 # z.a should be 42? - doesn't work!
This isn't possible in python - there's no "pointer" or "reference" data type. You can work around this by yielding a setter function instead of (or along with) the value:
class Z:
def __init__(self):
self.a, self.b, self.c = 1,2,3
def it(self):
for x in 'abc':
yield getattr(self, x), lambda y: setattr(self, x, y)
z = Z()
for x, setter in z.it():
if x == 1:
setter(42) # works!
Use enumerate()
to generate indices for you in the loop:
for i, bar in enumerate(foo):
foo[i] = bar + 42