On Wed, Jul 9, 2008 at 8:17 PM, Alan Gauld <[EMAIL PROTECTED]> wrote:
> No I meant in syntactic terms.
> We usually define an LC as
>
> [ expr for vars in sequence if expr ]
>
> or somesuch imprecise gobbledy gook ;-).
>
> Now we can define the generator expr (syntax) as
>
>       expr for vars in sequence if expr
> and the LC as
>
>       [ gen expr ]

The gen exp needs the parens. You could possibly have an intermediate
term that can be put inside () or [].

The actual formal syntax definitions for the two are slightly different:
http://docs.python.org/ref/lists.html
http://docs.python.org/ref/genexpr.html

Presumably this means there is something that is syntactically allowed
in one form and not the other, but I can't figure out what it might
be.

>> <code>
>> squares = (x * x for x in range (10))
>> l = [squares]
>
> But doesn't that generate a tuple (because of the parens)?

No, the parens are required for, and create, a generator expression.
In [23]: squares = (x * x for x in range (10))

In [24]: squares
Out[24]: <generator object at 0x126ae90>

Kent
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to