I am trying to expand a tuple in a yield statement but I'm getting a SyntaxError. Is this even possible, or is the * operator only for function calls?
Here is an example below:
class Inner(object):
def __init__(self):
self._vals = {'a': 1,
'b': 2,
'c': 3,
'd': 4}
def __iter__(self):
for key, val in self._vals.items():
yield key, val
class Outer(object):
def __init__(self):
self._morevals = {"foo": Inner(),
"bar": Inner()}
def __iter__(self):
for key, moreval in self._morevals.items():
for val in moreval:
yield key, *val # !!! <--- This is where the error is
object_under_test = Outer()
for outer_key, inner_key, inner_value in object_under_test:
print("{} {} {}".format(outer_key, inner_key, inner_value))
# Want:
# foo a 1
# foo b 2
# foo c 3
# foo d 4
# bar a 1
# bar b 2
# bar c 3
# bar d 4
Instead, I'm getting this error:
yield key, *val
^
SyntaxError: invalid syntax