Why is a generator expression called a expression?

Pieter van Oostrum pieter-l at vanoostrum.org
Tue Apr 21 09:43:09 EDT 2020


Veek M <veek at dont-use-this.com> writes:

> The docs state that a expression is some combination of value, operator, 
> variable and function. Also you cannot add or combine a generator 
> expression with a value as you would do with 2 + 3 + 4. For example, 
> someone on IRC suggested this
> all(a == 'a' for a in 'apple') but
>
> 1. all is a function/method
> 2. so (whatever) in this case is a call but obviously it works so it must 
> be a generator object as well.. so.. how does 'all' the function object 
> work with the generator object that's being produced? 
>
> I can't for example do min 1,2,3 but i can do min (1,2,3) and the () are 
> not integral to a tuple - therefore one could argue that the () are part 
> of the call - not so with a generator Expression where the () are 
> integral to its existence as an object.
>
> Could someone clarify further.

The Language Reference Manual says:

generator_expression ::=  "(" expression comp_for ")"

The parentheses can be omitted on calls with only one argument. See
section Calls for details.

Calls:

call ::=  primary "(" [argument_list [","] | comprehension] ")"
comprehension ::=  expression comp_for

The last part is the inner part (i.e. without the parentheses) of
generator_expression.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]


More information about the Python-list mailing list