Calling a generator multiple times

Bruce Eckel Bruce at EckelObjects.com
Thu Dec 6 22:37:43 CET 2001


That's exactly what I was looking for! Always seeing list
comprehensions described as:
[i for i in foo where bar]
I didn't realized that the leftmost element in the comprehension
was actually evaluated.

Thanks! New insights into comprehensions...

*********** REPLY SEPARATOR  ***********

On 12/6/01 at 1:30 PM Mitch Chapman wrote:

>Bruce Eckel wrote:
>> 
>> I'm trying to create a clever way to call a generator multiple
>> times, but the only thing I've been able to come up with is:
>> 
>> import random
>> rgen = random.Random()
>> def itemGenerator(dummy):
>>   return rgen.choice(['one', 'two', 'three', 'four'])
>> 
>> print map(itemGenerator, [None]* 25)
>> 
>> This works, but it requires the 'dummy' argument which seems
>> inelegant. I'll bet someone has a better idea...
>
>If you're using a recent Python release (>= 2.1?) you could
>use list comprehensions:
>
>
>def itemGenerator():
>    return rgen.choice(['one', 'two', 'three', 'four'])
>
>print [itemGenerator() for i in range(25)]
>
>-- 
>Mitch Chapman
>Mitch.Chapman at bioreason.com
>-- 
>http://mail.python.org/mailman/listinfo/python-list



Most current information can be found at:
http://www.mindview.net/Etc/notes.html
===================
Bruce Eckel    http://www.BruceEckel.com
Contains free electronic books: "Thinking in Java 2e" & "Thinking
in C++ 2e"
Please subscribe to my free newsletter -- just send any email to:
join-eckel-oo-programming at earth.lyris.net
My schedule can be found at:
http://www.mindview.net/Calendar
===================






More information about the Python-list mailing list