a break for comprehensions

Cliff Crawford cjc26 at nospam.cornell.edu
Tue Jul 31 19:27:48 EDT 2001


* Steve Holden <sholden at holdenweb.com> menulis:
| 
| I believe a separate syntax would be required to represent such a radically
| different object. But you, of course, may know better ... if you're looking
| for a syntax, one possibility would be:
| 
|     [[x * 2 for x in g()]]

Another possibility would be to have list comprehensions always return a
generator, and then if you need a list instead, use the built-in
function list(), for example:

    list([x*2 for x in g()])

But this will cause code breakage..maybe the solution to *that* would be
to add __getitem__(), __len__(), __contains__(), etc. to generator
objects, so that they can be manipulated as if they were lists.  (This
would solve the original poster's problem too -- you could do g()[:50]
to get the first 50 items of a generator.)


-- 
Cliff Crawford            http://www.sowrong.org/
A sign should be posted over every campus toilet:
"This flush comes to you by courtesy of capitalism." 
                                 -- Camille Paglia



More information about the Python-list mailing list