python-peps/pep-3142.txt

135 lines
3.8 KiB
Plaintext
Raw Normal View History

2009-01-18 05:28:20 -05:00
PEP: 3142
Title: Add a "while" clause to generator expressions
Version: $Revision$
Last-Modified: $Date$
Author: Gerald Britton <gerald.britton@gmail.com>
2013-05-10 12:27:55 -04:00
Status: Rejected
2009-01-18 05:28:20 -05:00
Type: Standards Track
Content-Type: text/x-rst
2009-01-18 05:28:20 -05:00
Created: 12-Jan-2009
Python-Version: 3.0
Post-History:
2013-05-10 12:27:55 -04:00
Resolution: http://mail.python.org/pipermail/python-dev/2013-May/126136.html
2009-01-18 05:28:20 -05:00
Abstract
========
2009-01-18 05:28:20 -05:00
This PEP proposes an enhancement to generator expressions, adding a
"while" clause to complement the existing "if" clause.
2009-01-18 05:28:20 -05:00
Rationale
=========
2009-01-18 05:28:20 -05:00
A generator expression (PEP 289 [1]_) is a concise method to serve
dynamically-generated objects to list comprehensions (PEP 202 [2]_).
Current generator expressions allow for an "if" clause to filter
the objects that are returned to those meeting some set of
criteria. However, since the "if" clause is evaluated for every
object that may be returned, in some cases it is possible that all
objects would be rejected after a certain point. For example::
2009-01-18 05:28:20 -05:00
g = (n for n in range(100) if n*n < 50)
2009-01-18 05:28:20 -05:00
which is equivalent to the using a generator function
(PEP 255 [3]_)::
2009-01-18 05:28:20 -05:00
def __gen(exp):
for n in exp:
if n*n < 50:
yield n
g = __gen(iter(range(10)))
2009-01-18 05:28:20 -05:00
would yield 0, 1, 2, 3, 4, 5, 6 and 7, but would also consider
the numbers from 8 to 99 and reject them all since ``n*n >= 50`` for
numbers in that range. Allowing for a "while" clause would allow
the redundant tests to be short-circuited::
2009-01-18 05:28:20 -05:00
g = (n for n in range(100) while n*n < 50)
2009-01-18 05:28:20 -05:00
would also yield 0, 1, 2, 3, 4, 5, 6 and 7, but would stop at 8
since the condition (``n*n < 50``) is no longer true. This would be
equivalent to the generator function::
2009-01-18 05:28:20 -05:00
def __gen(exp):
for n in exp:
if n*n < 50:
yield n
else:
break
g = __gen(iter(range(100)))
2009-01-18 05:28:20 -05:00
Currently, in order to achieve the same result, one would need to
either write a generator function such as the one above or use the
takewhile function from itertools::
2009-01-18 05:28:20 -05:00
from itertools import takewhile
g = takewhile(lambda n: n*n < 50, range(100))
2009-01-18 05:28:20 -05:00
The takewhile code achieves the same result as the proposed syntax,
albeit in a longer (some would say "less-elegant") fashion. Also,
the takewhile version requires an extra function call (the lambda
in the example above) with the associated performance penalty.
A simple test shows that::
2009-01-18 05:28:20 -05:00
for n in (n for n in range(100) if 1): pass
2009-01-18 05:28:20 -05:00
performs about 10% better than::
2009-01-18 05:28:20 -05:00
for n in takewhile(lambda n: 1, range(100)): pass
2009-01-18 05:28:20 -05:00
though they achieve similar results. (The first example uses a
generator; takewhile is an iterator). If similarly implemented,
a "while" clause should perform about the same as the "if" clause
does today.
2009-01-18 05:28:20 -05:00
The reader may ask if the "if" and "while" clauses should be
mutually exclusive. There are good examples that show that there
are times when both may be used to good advantage. For example::
2009-01-18 05:28:20 -05:00
p = (p for p in primes() if p > 100 while p < 1000)
2009-01-18 05:28:20 -05:00
should return prime numbers found between 100 and 1000, assuming
I have a ``primes()`` generator that yields prime numbers.
2009-01-18 05:28:20 -05:00
Adding a "while" clause to generator expressions maintains the
compact form while adding a useful facility for short-circuiting
the expression.
2009-01-18 05:28:20 -05:00
Acknowledgements
================
2009-01-18 05:28:20 -05:00
Raymond Hettinger first proposed the concept of generator
expressions in January 2002.
2009-01-18 05:28:20 -05:00
References
==========
2009-01-18 05:28:20 -05:00
.. [1] PEP 289: Generator Expressions
2009-01-18 05:28:20 -05:00
http://www.python.org/dev/peps/pep-0289/
.. [2] PEP 202: List Comprehensions
2009-01-18 05:28:20 -05:00
http://www.python.org/dev/peps/pep-0202/
.. [3] PEP 255: Simple Generators
2009-01-18 05:28:20 -05:00
http://www.python.org/dev/peps/pep-0255/
Copyright
=========
2009-01-18 05:28:20 -05:00
This document has been placed in the public domain.
2009-01-18 05:28:20 -05:00
..
Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
coding: utf-8
End: