Nick Coghlan wrote: > > Josiah Carlson wrote: > >> Similarly, a 3.x list comprehension [i*i for i in x] is very roughly > >> translated as: > >> > >> def _lc(arg): > >> result = [] > >> for i in arg: > >> result.append(i*i) > >> return result > >> > >> <expr_value> = _lc(x) > > > > I was under the impression that in 3.x, it was equivalent to > > list(<genexp>) . Which would explain the difference between 2.6 and > > 3.0. > > Semantically, the two translations end up being the same.
Actually, they're not quite the same. list(<genexp>) will swallow any StopIteration exceptions that end up getting thrown inside the generator, but even in Python 3, list comprehensions will not catch StopIteration. So for explanatory purposes, thinking of a list comprehension as a function that returns a list is more useful than thinking of it as list(<genexp>). -- Carl _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com