# why is the following invalid
x = (k, v for k, v in some_dict.items())
# but if we wrap the expression part in parentheses it works
x = ((k, v) for k, v in some_dict.items())
After reviewing the documentation, I couldn't find any information on this issue. What could be causing confusion for the parser to the extent that the syntax is not permitted? This seems strange, since despite that, more complex syntax works just fine:
# k, v somehow confuses the parser but this doesn't???
x = ('%s:%s:%s' % (k, v, k) for k, v in some_dict.items())
If there is actually ambiguity. How come we don't also need to wrap %s:%s:%s % (k, v, k)
with a surrounding parentheses too then?
Look at x = (k, v for k, v in some_dict.items())
:
x = (k, v for k, v in some_dict.items())
x = ((k, v) for k, v in some_dict.items())
x = (k, (v for k, v in some_dict.items()))
Parentheses are needed to remove the ambiguity.
x = ('%s:%s:%s' % (k, v, k) for k, v in some_dict.items())
requires parentheses too:
x = ('%s:%s:%s' % k, v, k for k, v in some_dict.items())
x = ('%s:%s:%s' % k, (v, k) for k, v in some_dict.items())
x = ('%s:%s:%s' % (k, v, k) for k, v in some_dict.items())
It just so happens that you already had enough parentheses to resolve the ambiguity there in a way that allowed it to run in the expected manner.