PEP 289: Generator Expressions (please comment)

Emile van Sebille emile at fenx.com
Fri Oct 24 11:58:04 EDT 2003


Raymond Hettinger revives pep 289:
> Peter Norvig's creative thinking triggered renewed interest in PEP 289.
> That led to a number of contributors helping to re-work the pep details
> into a form that has been well received on the python-dev list:
>
>     http://www.python.org/peps/pep-0289.html
>
> In brief, the PEP proposes a list comprehension style syntax for
> creating fast, memory efficient generator expressions on the fly:

What do you get from:
    type(x*x for x in roots)

I assume you'd get something like genexp that behaves like an iterator and
that the consuming function accesses items until StopIteration(StopGenxing?
;) is raised, and that a genexp is otherwise a first class object.

>From the PEP:
"All free variable bindings are captured at the time this function is
defined, and passed into it using default argument values...[snip]...In
fact, to date, no examples have been found of code where it would be better
to use the execution-time instead of the definition-time value of a free
variable."

Does this imply then that instead of:
dumprec = "\n".join('%s : %s' % (fld.name, fld.val) for fld in rec)
for rec in file: print dumprec

one would need to write some variation of:
for rec in file:
    print "\n".join('%s : %s' % (fld.name, fld.val) for fld in rec)

or:
def dumprec(rec):
    return "\n".join('%s : %s' % (fld.name, fld.val) for fld in rec)
for rec in file: print dumprec(rec)

I find the first construct easier on the eyes, and it fulfills on the
promise of 'generator expression' as opposed to 'iterable generated
expression'.  Although it does feel much more macro-ish...

Overall, +1.


Emile van Sebille
emile at fenx.com






More information about the Python-list mailing list