What are python closures realy like?

Fredrik Lundh fredrik at pythonware.com
Tue Dec 12 02:44:03 EST 2006


John Nagle wrote:

>     Most of the examples given here are kind of silly, but closures have
> real uses.  I used one today in Javascript because I was writing an
> AJAX application, and I was using an API, the standard XMLHttpRequestObject,
> which required a callback function with no arguments.  A closure allowed
> the code to pass relevant information with the callback function, which
> would be called when a request to the server completed.  A global
> variable wouldn't have worked, because multiple instances of the object
> making the callback are possible.

the usual way of handling multiple callback context instances in Python 
is of course to create multiple instances of the class that implements 
the behaviour, and use a bound method as the callback.

</F>




More information about the Python-list mailing list