On Nov 5, 4:56 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Wed, 05 Nov 2008 14:39:36 +1100, Ben Finney wrote: > > Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> writes: > > >> On Wed, 05 Nov 2008 13:18:27 +1100, Ben Finney wrote: > > >> > Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> writes: > > >> > Your example shows only that they're important for grouping the > >> > expression from surrounding syntax. As I said. > > >> > They are *not* important for making the expresison be a generator > >> > expression in the first place. Parentheses are irrelevant for the > >> > generator expression syntax. > > >> Okay, technically correct but parenthesis belong to generator > >> expressions because they have to be there to separate them from > >> surrounding syntax with the exception when there are already enclosing > >> parentheses. So parenthesis are tied to generator expression syntax. > > > No, I think that's factually wrong *and* confusing. > > > >>> list(i + 7 for i in range(10)) > > [7, 8, 9, 10, 11, 12, 13, 14, 15, 16] > > > Does this demonstrate that parentheses are “tied to” integer literal > > syntax? No. > > You can use integer literals without parenthesis, like the 7 above, but > you can't use generator expressions without them. They are always > there. In that way parenthesis are tied to generator expressions. > > If I see the pattern ``f(x) for x in obj if c(x)`` I look if it is > enclosed in parenthesis or brackets to decide if it is a list > comprehension or a generator expression. That may not reflect the formal > grammar, but it is IMHO the easiest and pragmatic way to look at this as > a human programmer. > > Ciao, > Marc 'BlackJack' Rintsch
The situation is similar to tuples. What makes a tuple is the commas, not the parens. 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. -- http://mail.python.org/mailman/listinfo/python-list