Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80406
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'debugging': 0.07; 'expressions': 0.07; 'problem?': 0.07; 'constructor': 0.09; 'difference,': 0.09; 'raises': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'times,': 0.14; 'code?': 0.16; 'expressions,': 0.16; 'iterable': 0.16; 'iterable)': 0.16; 'iterator': 0.16; 'phase,': 0.16; 'raised.': 0.16; 'received:80.91.229.3': 0.16; 'received:mediaways.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:pool.mediaways.net': 0.16; 'silly': 0.16; 'subject:Different': 0.16; 'subject:expressions': 0.16; 'subject:generator': 0.16; 'wrote:': 0.18; 'example': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'equivalent': 0.26; 'handling': 0.26; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'subject:list': 0.30; '(which': 0.31; 'code': 0.31; '(since': 0.31; "d'aprano": 0.31; 'follows': 0.31; 'raised': 0.31; 'steven': 0.31; 'anyone': 0.31; "can't": 0.35; 'agree': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'so,': 0.37; 'list': 0.37; 'level': 0.37; 'to:addr :python-list': 0.38; 'rather': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'how': 0.40; 'expression': 0.60; 'tell': 0.60; 'simple': 0.61; 'skip:n 10': 0.64; 'yes': 0.68; 'difficulty': 0.68; 'caused': 0.69; 'received:93': 0.72; 'behavior': 0.77; 'hand': 0.80; 'leak': 0.84; 'protocol,': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
| Subject | Re: Different behaviour in list comps and generator expressions |
| Date | Sat, 08 Nov 2014 22:31:23 +0100 |
| References | <545d76fe$0$12980$c3e8da3$5496439d@news.astraweb.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | frbg-5d84fba7.pool.mediaways.net |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 |
| In-Reply-To | <545d76fe$0$12980$c3e8da3$5496439d@news.astraweb.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15588.1415482299.18130.python-list@python.org> (permalink) |
| Lines | 73 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1415482299 news.xs4all.nl 2956 [2001:888:2000:d::a6]:47037 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:80406 |
Show key headers only | View raw
On 08.11.2014 02:50, Steven D'Aprano wrote:
> The following list comprehension and generator expression are almost, but
> not quite, the same:
>
> [expr for x in iterable]
>
> list(expr for x in iterable)
>
>
> The difference is in the handling of StopIteration raised inside the expr.
> Generator expressions consume them and halt, while comprehensions allow
> them to leak out.
This is not the right description of what's happening. It is not the
generator expression that consumes it, but the list constructor and, of
course, list can't tell at which level in the inside code StopIteration
got raised.
So, yes this had me confused some times, too, but it is really not
surprising.
> A simple example:
>
> iterable = [iter([])]
> list(next(x) for x in iterable)
> => returns []
>
> But:
>
> [next(x) for x in iterable]
> => raises StopIteration
>
Yes, but the equivalent to list(next(x) for x in iterable) is:
l = []
for item in (next(x) for x in iterable):
l.append(item)
=> returns [] because the for consumes the StopIteration
this, on the other hand raises StopIteration
l = []
for item in [next(x) for x in iterable]:
l.append(item)
because the error gets raised already when for causes iter() to be
called on the list comprehension.
>
> Has anyone come across this difference in the wild? Was it a problem? Do you
> rely on that difference, or is it a nuisance? Has it caused difficulty in
> debugging code?
>
> If you had to keep one behaviour, which would you keep?
>
The point is: there is no difference in the behavior of comprehensions
and generator expressions, it is just how you access them:
for anything that follows the iterator protocol, a comprehension (since
it is evaluated immediately) raises during the iter() phase, while a
generator expression (which gets evaluated lazily) raises during the
next() phase where it gets swallowed.
So in your example you would have to use the silly
list([next(x) for x in iterable])
if you want the error to get raised.
I agree this is all rather non-intuitive, but how would you change it ?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Different behaviour in list comps and generator expressions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-08 12:50 +1100
Re: Different behaviour in list comps and generator expressions Roy Smith <roy@panix.com> - 2014-11-07 23:39 -0500
Re: Different behaviour in list comps and generator expressions Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-11-08 22:31 +0100
Re: Different behaviour in list comps and generator expressions Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-11-08 23:42 +0100
Re: Different behaviour in list comps and generator expressions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-18 10:30 +1100
Re: Different behaviour in list comps and generator expressions Dan Stromberg <drsalists@gmail.com> - 2014-11-17 15:38 -0800
Re: Different behaviour in list comps and generator expressions Ethan Furman <ethan@stoneleaf.us> - 2014-11-17 18:20 -0800
csiph-web