[Tutor] What is this an example of (and how can i use it?)

kevin parks kp8 at mac.com
Sun Sep 20 19:27:34 CEST 2009


On Sep 21, 2009, at 1:32 AM, Alan Gauld wrote:

> kevin parks wrote:
>
>> called, and what it is an example of. I guess there are generators  
>> and iterators now and it seems this might be an example of one of  
>> those new
>
> This is a generator expression.

That's unfortunate news for me.

> It is like a list comprehension (you know about those right?)

Yes. I know and use and love them daily. Even if there were  
implemented backwards :)
[for x in range(10) x**2] would have been easier than: [x**2 for x in  
range(10)] But i am used to it now.


> except it doesn't create the list it just returns each item on  
> demand. You could think of a list as a list constructed using a  
> generator expression.
>
>> def roundrobin(*iterables):
>>    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
>>    # Recipe credited to George Sakkis
>>    pending = len(iterables)
>>    nexts = cycle(iter(it).next for it in iterables)
>
> note this is storing the next methods not the results of them.
>
>>    while pending:
>>        try:
>>            for next in nexts:
>>                yield next()
>
> So the yield calls the stored method and returns the result.


So... then to call (by call i mean use/execute/doit) i would do, what?  
foo.next()

I kinda understand conceptually what iterators and generators do and  
why they are "a honking good idea" (why create 100 of x when we just  
want the 100th, etc.) what i don't get is the syntax and how they are  
used in real life. How generator and iterators behave in the wild. I  
am also bummed since generators have methods, which means they are OO  
which means i am i'd be in for 16 years of computer science study and  
super arcane, obscure and opaque concepts like what to do with  
__self__ and all that junk before i can use them.

Anyway i needed a pea shooter that does a round robin. This one does  
it, but i don't know how to use it.

I read up on gennies and itties and see if i can get my head around  
it. They are really poorly addressed nearly everywhere i look. They  
are explained so that really smart folks who know a lot of CS and are  
fluent in 15 computer languages can understand them, but not us  
mortals. Even the Lutz is too terse and generally poor on these two  
complex and relatively new constructs. They are a dark and obscure  
magic.  I'll try the links Kent pointed me to first and see how that  
goes.

thanks,

-kp--






















More information about the Tutor mailing list