This following part is copied from the official, complete Python 3 grammar .
arglist: argument (',' argument)* [',']
argument: ( test [comp_for] |
test ':=' test |
test '=' test |
'**' test |
'*' test )
comp_iter: comp_for | comp_if
sync_comp_for: 'for' exprlist 'in' or_test [comp_iter]
comp_for: [ASYNC] sync_comp_for
comp_if: 'if' test_nocond [comp_iter]
(arglist
is used in multiple places where you'd pass arguments to a function call or class definition).
I am confused by the rule argument : test [comp_for]
. As . for . in .
generators are already valid derivations of test
, this seems to imply that a for
as a function argument has a special meaning.
my_function(x for x in range(3))
However, just playing around with this definition always interpreted it as the usual generator object that is created by the described syntax.
y = (x for x in range(3))
my_function(y)
So, is there actually anything special about that syntax? Or is it just legacy / future reserved code?