list comprehensions to effect visitors

Neil Schemenauer nas at python.ca
Fri Dec 7 09:52:37 EST 2001


Bruce Eckel wrote:
> import random
> rgen = random.Random()
> flwrs = [Gladiolus(),Runuculus(),Chrysanthemum()]
> flowers = [rgen.choice(flwrs) for i in range(10)]
> bee = Bee()
> fly = Fly()
> worm = Worm()
> [(f.pollinate(bee), f.pollinate(fly), f.eat(worm)) for f in
> flowers]

Since you are not using the list produced by the last line it's probably
clearer to use a for loop:

    for f in flowers:
        f.pollinate(bee)
        f.pollinate(fly)
        f.eat(worm)

Listcomps are cool but don't go overboard. :-)

  Neil




More information about the Python-list mailing list