*expression and iterables

Steven Bethard steven.bethard at gmail.com
Fri Aug 20 02:09:36 EDT 2004


So I was looking at the Python 3.0 wiki
(http://www.python.org/moin/Python3_2e0) and noticed that one of the
major changes would be that builtin classes and functions would take
and return iterators instead of lists. Along these same lines, I'm
wondering if we could also add the feature that a *expression
parameter produces an iterable instead of a tuple.

Right now, using the *expression syntax with an iterable causes that
iterable to be consumed at the time of the function call, e.g.:

>>> def rangegen(n):
...     for i in range(n):
...             yield i
...     print "exhausted"
...
>>> def f(*args):
...     print "f"
...     for arg in args:
...             print arg
...
>>> f(*rangegen(3))
exhausted
f
0
1
2

It would be nice if the iterable was only consumed as necessary, e.g.:

>>> f(*rangegen(3))
f
0
1
2
exhausted

This would mean that izip(*izip(x)) would work without reading all the
elements of izip(x) into memory.

If I understand (http://docs.python.org/ref/calls.html) correctly,
parameter values are filled with the following sequence:
* use values from positional arguments
* use values from *expression argument
* use values from keyword arguments
* use values from **expression argument
So either the *expression iterable gets run to completion, fills some
parameter values, and the sequence proceeds as normal, or it runs
until all parameter values are filled, and then it's passed in as the
*args parameter.  Doesn't seem too unreasonable...

Is this implementable?  If so, it seems desirable -- is it something
that could be added to the Python 3.0 wiki?  (I'd love it now, of
course, but it's backwards incompatible =)

Steve

P.S. I didn't add it to the wiki myself because it sorta seemed like
that wiki was intended only for the goals GvR had already suggested.



More information about the Python-list mailing list