Sending changed parameters into nested generators

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Nov 12 23:43:48 EST 2010


On Fri, 12 Nov 2010 09:47:26 +0000, John O'Hagan wrote:

> I have a generator function which takes as arguments another generator
> and a dictionary of other generators like this:
> 
> def modgen(gen, gendict):
>     for item in gen():
> 	for k, v in gendict:
> 		do_something_called_k(item, v.next())
> 	yield item
[snip]
> If anyone's still reading :) , how can I send new values to arbitrary
> sub- generators?

I have a headache after reading your problem :(

I think it's a good time to point you at the Zen, particularly these five 
maxims:

Beautiful is better than ugly.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
If the implementation is hard to explain, it's a bad idea.


I'm afraid that your nested generators inside another generator idea 
fails all of those... it's not elegant (beautiful), it's complicated, 
it's nested, and the implementation is hard to explain.

You could probably replace generators with full-blown iterators, but I 
wonder what you're trying to accomplish that is so complicated that it 
needs such complexity to solve it. What are you actually trying to 
accomplish? Can you give a simple example of what practical task you hope 
to perform? I suspect there's probably a more elegant way to solve the 
problem.


-- 
Steven



More information about the Python-list mailing list