[Tutor] What is this [] construction?
Kent Johnson
kent37 at tds.net
Wed Mar 4 13:33:48 CET 2009
On Wed, Mar 4, 2009 at 2:56 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> In Python v3 list comprehensions and generator expressions have been
> "merged" in that putting a GE inside [] has the same effect as a LC. In
> practice this makes little or no difference to the programmer its just how
> Python handles it behind the scenes.
??
Python 3.0.1 (r301:69556, Feb 14 2009, 22:08:17)
>>> [x*x for x in range(3)]
[0, 1, 4]
>>> [(x*x for x in range(3))]
[<generator object <genexpr> at 0x6779b8>]
I think you mean this (from http://docs.python.org/3.0/whatsnew/3.0.html):
Also note that list comprehensions have different semantics: they are
closer to syntactic sugar for a generator expression inside a list()
constructor, and in particular the loop control variables are no
longer leaked into the surrounding scope.
Kent
More information about the Tutor
mailing list