Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue14845] list(<generator expression>) != [<list comprehension>]

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

May 17, 2012, 3:47 PM

Post #1 of 7 (67 views)
Permalink
[issue14845] list(<generator expression>) != [<list comprehension>]

New submission from Peter Norvig <pnorvig [at] google>:

PEP 289 says "the semantic definition of a list comprehension in Python 3.0 will be equivalent to list(<generator expression>). Here is a counterexample where they differ (tested in 3.2):

def five(x):
"Generator yields the object x five times."
for _ in range(5):
yield x


# If we ask five() for 10 objects in a list comprehension,
# we get an error:

>>> F = five('x')
>>> [next(F) for _ in range(10)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration

# But if we ask five() for 10 objects in a list(generator expr),
# we get five objects, no error:

>>> F = five('x')
>>> list(next(F) for _ in range(10))
['x', 'x', 'x', 'x', 'x']

----------
components: None
messages: 161023
nosy: Peter.Norvig
priority: normal
severity: normal
status: open
title: list(<generator expression>) != [<list comprehension>]
type: behavior
versions: Python 3.2

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14845>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 17, 2012, 3:51 PM

Post #2 of 7 (66 views)
Permalink
[issue14845] list(<generator expression>) != [<list comprehension>] [In reply to]

Changes by Ezio Melotti <ezio.melotti [at] gmail>:


----------
components: +Interpreter Core -None
nosy: +ezio.melotti
stage: -> needs patch
versions: +Python 3.3

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14845>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 17, 2012, 7:26 PM

Post #3 of 7 (61 views)
Permalink
[issue14845] list(<generator expression>) != [<list comprehension>] [In reply to]

R. David Murray <rdmurray [at] bitdance> added the comment:

I think the behavior is correct. next(x) is outside the for expression in the list comprehension, but 'list(x)' is an implicit 'for x in exp'. So I believe it is the doc that needs amplification. The PEP discussion is referring specifically to the leaking of the loop variable.

----------
nosy: +r.david.murray

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14845>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 17, 2012, 9:22 PM

Post #4 of 7 (63 views)
Permalink
[issue14845] list(<generator expression>) != [<list comprehension>] [In reply to]

Peter Norvig <pnorvig [at] google> added the comment:

I agree with R. David Murray -- if "correct" means following the PEP 289 semantics, then

list(next(F) for _ in range(10))

should be the same as

def __gen(exp):
for _ in exp:
yield next(F)

list(__gen(iter(range(10))))

and indeed that is the case. So the behvavior is correct and the documentation is both wrong, and rather informal/incomplete.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14845>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 18, 2012, 1:50 PM

Post #5 of 7 (62 views)
Permalink
[issue14845] list(<generator expression>) != [<list comprehension>] [In reply to]

Terry J. Reedy <tjreedy [at] udel> added the comment:

It has been noted elsewhere (but I cannot find it) that 1) an uncaught StopIteration raised in an expression *within* a genexp is indistinguishable from the StopIteration raised *by* the genexp upon normal termination; and 2) this makes genexps different from otherwise equivalent comprehensions. The statement in PEP289 does not address this exceptional case.

Raymond, do you want to revise your PEP to mention this?

PEPs are, as their name says, proposals that express intention, not result, and especially not the result after continued patching. We do not usually usually go back and revise PEPs after they have been implemented. It would be an endless task. Hence, they are not current 'documentation' unless unless specifically referred from the docs. This one is not so referenced.

Perhaps PEP 0 should say that " Finished PEPs (done, implemented in code repository)" are historical documents and not current documentation.

I checked the entries for comprehensions and generator expressions and they do not claim equivalence, but do (briefly) describe the actual behavior. However, it is easy to miss that the entry in 5.2.8. Generator expressions implies the two facts above. Perhaps we should explicitly say something like:

If *expression* raises StopIteration, it will not be distinguished from the StopIteration raised upon normal termination of the generator, and it will make the generator expression act differently from the otherwise equivalent comprehension.

----------
assignee: -> docs [at] pytho
components: +Documentation -Interpreter Core
nosy: +docs [at] pytho, rhettinger, terry.reedy

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14845>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 19, 2012, 3:49 AM

Post #6 of 7 (62 views)
Permalink
[issue14845] list(<generator expression>) != [<list comprehension>] [In reply to]

Changes by Chris Rebert <pybugs [at] rebertia>:


----------
nosy: +cvrebert

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14845>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 20, 2012, 12:14 AM

Post #7 of 7 (61 views)
Permalink
[issue14845] list(<generator expression>) != [<list comprehension>] [In reply to]

Raymond Hettinger <raymond.hettinger [at] gmail> added the comment:

I will be happy to clarify the PEP when I get a chance.

----------
assignee: docs [at] pytho -> rhettinger
priority: normal -> low

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14845>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.