Consider the following Python code:
def f(*args):
for a in args:
pass
foo = ['foo', 'bar', 'baz']
# Python generator expressions FTW
gen = (f for f in foo)
f(*gen)
Does *args
automatically expand the generator at call-time? Put another way, am I iterating over gen
twice within f(*gen)
, once to expand *args
and once to iterate over args? Or is the generator preserved in pristine condition, while iteration only happens once during the for loop?
f(*gen)
a second time returns()
, to show that invokingf
exhausts the generator for subsequent lines. – Expression