Is there a better/simpler way to filter blank lines?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Nov 5 17:05:13 EST 2008


On Wed, 05 Nov 2008 21:23:57 +0000, Arnaud Delobelle wrote:

> Lie <Lie.1296 at gmail.com> writes:
>> What makes a generator expression is "<exp> for <var-or-tuple> in
>> <exp>".
>>
>> Parenthesis is generally required because without it, it's almost
>> impossible to differentiate it with the surrounding. But it is not part
>> of the formally required syntax.
> 
> ... But *every* generator expression is surrounded by parentheses, isn't
> it?

Yes, but sometimes they are there in order to call a function, not to 
form the generator expression.

I'm surprised that nobody yet has RTFM:

http://docs.python.org/reference/expressions.html

[quote]
A generator expression is a compact generator notation in parentheses:

generator_expression ::=  "(" expression genexpr_for ")"
genexpr_for          ::=  "for" target_list "in" or_test [genexpr_iter]
genexpr_iter         ::=  genexpr_for | genexpr_if
genexpr_if           ::=  "if" old_expression [genexpr_iter]

...
The parentheses can be omitted on calls with only one argument.
[end quote]

It seems to me that the FM says that the parentheses *are* part of the 
syntax for a generator expression, but if some other syntactic construct 
(e.g. a function call) provides the parentheses, then you don't need to 
supply a second, redundant, pair.

I believe that this is the definitive answer, short of somebody reading 
the source code and claiming the documentation is wrong.



-- 
Steven



More information about the Python-list mailing list