PEP 289: Generator Expressions (please comment)

Alex Martelli aleax at aleax.it
Tue Oct 28 06:32:01 EST 2003


Patrick Maupin wrote:
   ...
> I think that once the new generator expressions are available
> (if it happens in the form as proposed), I will actually be
> _converting_ a lot of list comprehensions into generator-expression
> form as I do code maintenance.

There may be a case for converting EVERY LC to a genexp -- if
you do need the list object, just use list(...) where today you'd
use the [...] notation.  4 more characters, and won't work in 2.3
(i.e. you'd be trying your code to 2.4 or better), but I do
understand the desirability of having "just one obvious way"
to do some GENERAL task --

"make a container x of type X with contents given by genexp G"
  ===
x = X(G)

uniformly, rather than specialcasing X==list as "x = [G]" and
thus making it non-uniform to dict, sets.Set, tuple, ...

For similar reasons, I now prefer to accomplish the general task:

"make a shallow copy y of container x which is of type X"

as

y = X(x)

[in those cases where y = copy.copy(x) would be inappropriate,
i.e., i know X and _want_ to hardcode it in my code for clarity)

rather than specialcasing "y=x[:]" when X==list, "y=x.copy()"
when X==dict, ...


Alex





More information about the Python-list mailing list