passing parameters to generators ?

Emile van Sebille emile at fenx.com
Sun Nov 4 08:36:04 EST 2001


"Jan Samohyl" <asgard at hellnet.cz> wrote in message
news:mailman.1004878271.8696.python-list at python.org...
> Hello,
> I would like to pass different parameters to a generator while it's
generating, is there some simple way to do that?
> I want to write a synchronous server, that would go through all the active
connections and when the client response is
> available, it would call a generator with the client response, and resume
the conversation with the client exactly at the
> point it yielded out to let other talk. I think it would be very cool to
do it this way, but unfortunately, current generator
> implementation doesn't allow to do it that simply. Why is a function with
yield *always* a generator factory? Why it isn't
> simply the generator, and generator factory or iterator would be done as a
function factory returning generator next()? And
> when you would call generator again with different parameters, they would
be different inside the generator (but the locals
> would stay the same and you would enter behind the last yield statement).
>

This came up earlier this week.  Here's my response to the earlier question:

You can share mutable objects to pass in information:

def gf(args):
    while 1:
        yield args[0]*args[1]

a = [2,3]
g = gf(a)
print g.next()
a[1]=4
print g.next()

Is this what you're looking for?

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list