creating generators from function
Steven Bethard
steven.bethard at gmail.com
Wed Dec 8 04:47:38 EST 2004
More information about the Python-list mailing list
Wed Dec 8 04:47:38 EST 2004
- Previous message (by thread): creating generators from function
- Next message (by thread): creating generators from function
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Simon Wittber wrote:
> I use a coroutine/generator framework for simulating concurrent processes.
>
> To do this, I write all my functions using the general form:
>
> while True:
> do stuff
> yield None
>
> To make these generator functions compatible with a standard thread
> interface, I attempted to write a decorator which converts a standard
> function into a generator function (code attached below).
[snip]
> @generatorize
> def f():
> while True:
> return 1
>
> def g():
> while True:
> yield 1
>
> print g()
> print f()
I'm a little confused as to what you're trying to do here. The f()
function, in particular, doesn't make much sense -- the 'while True'
doesn't do anything since the 'return 1' is executed on the first time
through the loop. If you really do want to turn your functions into
generators of the form:
while True:
do stuff
yield None
why can't you just do:
>>> def generatorize(f):
... def new_f(*args, **kwds):
... while True:
... f(*args, **kwds)
... yield None
... return new_f
...
>>> @generatorize
... def f():
... return 1
...
>>> i = f()
>>> print i.next()
None
>>> print i.next()
None
Basically, I've assumed that 'do stuff' is the function that's being
wrapped.
Steve
- Previous message (by thread): creating generators from function
- Next message (by thread): creating generators from function
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list