When is List Comprehension inappropriate?

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Tue Mar 20 04:02:54 EDT 2007


BJörn Lindqvist:
> While they
> may be faster, Psyco is great here. Also, if you have lots of 2d-loops
> like "for x in something: for y in something:", then it could be more
> beautiful to separate the iteration from the task:
>
> def iterimage(im, width, height, step = 1):
>     for y in range(0, height, step):
>         for x in range(0, width, step):
>             yield (x, y), im.getpixel((x, y))

Just a note: Psyco usually isn't able to speed up generators (ShedSkin
recently has hadded a support of them too, and it seem fast enough).

Bye,
bearophile




More information about the Python-list mailing list