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

Alan Gauld alan.gauld at btinternet.com
Mon Sep 21 02:20:26 CEST 2009


kevin parks wrote:

>> 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. 

If you can grok comprehensions then you are only a step away from 
generator expressions. Most of the time you don't need to think about 
the hairy CS stuff underneath, just use them. I found the jump to using 
comprehensions much bigger than the jump from them to generator expressions!

>> You could think of a list as a list constructed using a generator 
>> expression.

should have been ....a list comprehension as a list....

>>>    nexts = cycle(iter(it).next for it in iterables)
>>
>> note this is storing the next methods not the results of them.
>>
>>>    while pending:
>>>            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()

That depends on what next() actually returns.
e started off with a list of iterables (eg files strings, lists, tuples 
- possibly a mixture of them. We then build a list of their next 
methods.  So when we call this methods we get back an instance of 
whatever was in the original list of iterators.
Hee isa slightly less complex example:

 >>> its = [[1,2,3],'abc']
 >>> nxts = [iter(n).next for n in its]
 >>> for repeats in range(2):
	for n in nxts:
		print n()

		
1
a
2
b

So the first for n loop printed out the first item in each iterable,
the next time round we printed the second item etc.

Each iteration prints a number and a letter
So when your yield returns its value you have to do with it
whatever is appropriate!

> 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. 

ignore the memory management magic and just think of them returning the 
next item in a list on demand. Have you ever used xrange() instead of 
range()? Its the same thing.

> bummed since generators have methods, which means they are OO 

So do lists but you said you use lists every day! You use objects 
everywhere in Python, everything is an object and has methods.
try:

 >>> dir(6)

don't be freaked by objects, you use them already every day.

> 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 

You can do all sorts of clever black magic type stuff in Python even 
without OOP but mostly you don't need to, just keep it simple.

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

Actually the example you quoted is a fairly sophisticated bit of code 
that probably does more than you need. What exactly are you trying to 
achieve, there is probably an easier way!

> 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 

They are difficult conceptually, but if you concentrate on the examples 
of use and don't try to be too ambitious initially they will grow on you 
like comprehensions did. And remember, you never need them. There are 
always alternative ways to do it, just using a bit more code usually.

HTH,

Alan G.
http://www.alan-g.me.uk



More information about the Tutor mailing list