[Python-ideas] How can I get own instance of generator in itself code
Simon Sapin
simon.sapin at kozea.fr
Sun Jul 15 11:36:27 CEST 2012
Le 15/07/2012 08:18, Đinh Nho Nam a écrit :
> def invoke_callback(url):
> do something here
> self = get myself (invoke_callback generator) instance #how can we do as this line????
> data = yield asynchronous_dowload(url, callback = self.next)
> do something with data
>
> How can we do as this code. How can I get self variable?. Does the
> language support this feature? I cannot find it in any document in
> python.org. So, I have to solve it by wrapper it in a list.
Hi,
Maybe you could achieve that by inspecting the stack frame, but this is
generally not recommended.
But taking a step back, I think that the primitive you are looking for
is a more general coroutine. greenlet can do this, and gevent builds on
it to add higher level async networking:
http://greenlet.readthedocs.org/en/latest/index.html
http://www.gevent.org/
Regards,
--
Simon Sapin
More information about the Python-ideas
mailing list