[Python-Dev] FW: list-display semantics?

Greg Ewing greg@cosc.canterbury.ac.nz
Mon, 11 Jun 2001 14:44:54 +1200 (NZST)


parywu@seed.net.tw:

> [x for x in [1, 2, 3], y for y in [4, 5, 6]]
> and the result surprised me, that is:
> [[1,2,3],[1,2,3],[1,2,3],9,9,9]

Did you by any chance execute that in an environment
where y was previously bound to 9?

It will be parsed as

   [x for x in ([1, 2, 3], y) for y in [4, 5, 6]]

which should give a NameError if y is previously unbound, 
since it will try to evaluate ([1, 2, 3], y) before y is
bound by the inner loop.

But executing y = 9 beforehand will give the results
you got.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg@cosc.canterbury.ac.nz	   +--------------------------------------+