BUG? list-comprehension's inconsistency? was: Re: list-display semantics?

Roman Suzi rnd at onego.ru
Sun Jun 10 21:00:30 CEST 2001


On Mon, 11 Jun 2001, jainweiwu wrote:

BTW, I think it could be list-comprehension's
inconsistency (if not a bug!), look:

1->>> [x for x in [1, 2, 3], y for y in [4, 5, 6]]        # first try
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'y' is not defined
2->>> [x for x in [1, 2, 3], [y for y in [4, 5, 6]]]
[[1, 2, 3], [4, 5, 6]]
3->>> [[x for x in [1, 2, 3]], [y for y in [4, 5, 6]]]
[[1, 2, 3], [4, 5, 6]]
4->>> [x for x in [1, 2, 3], y for y in [4, 5, 6]]        # 2nd try
[[1, 2, 3], [1, 2, 3], [1, 2, 3], 6, 6, 6]

-- and this lead to subtle errors.

There are even two things which seem wrong here:

A) 1 and 4-s steps differ only because y is now defined.
B) results of step 2 and 3 do not differ, while visually
I've added [] around first item.

(it was a discovery to me to know that I can delimit
naked list comprehensions by "," inside list definitions!)

Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Sunday, June 10, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "Computers make very fast, very accurate mistakes." _/





More information about the Python-list mailing list