A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2009-February/086349.html below:

[Python-Dev] Seeming unintended difference between list comprehensions and generator expressions...

[Python-Dev] Seeming unintended difference between list comprehensions and generator expressions... [Python-Dev] Seeming unintended difference between list comprehensions and generator expressions...Carl Johnson cmjohnson.mailinglist at gmail.com
Sun Feb 22 09:17:02 CET 2009
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
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4