Turning a callback function into a generator

Peter Otten __peter__ at web.de
Mon Jul 3 03:19:16 EDT 2006


Kirk McDonald wrote:

> Let's say I have a function that takes a callback function as a
> parameter, and uses it to describe an iteration:
> 
> def func(callback):
>      for i in [1, 2, 3, 4, 5]:
>          callback(i)
> 
> For the sake of argument, assume the iteration is something more
> interesting than this which relies on the callback mechanism. The
> function is an existing interface, and I cannot change it.
> 
> I want to somehow, in some way, provide an iteration interface to this
> function. Thoughts?

I don't think there is a robust solution, see

http://groups.google.com/group/comp.lang.python/browse_frm/thread/0ce55373f128aa4e/1d27a78ca6408134?&hl=en
aka http://mail.python.org/pipermail/python-list/2003-December/197726.html

http://groups.google.com/group/comp.lang.python/browse_frm/thread/5793298a1ce93292/9230ddcc55d84694?&hl=en
aka http://mail.python.org/pipermail/python-list/2004-September/239025.html

Peter




More information about the Python-list mailing list