farmdev

Python gets true closures in 3000 - do I care?

Really the only thing stopping python 2.x from having closures like how Martin Fowler defines them is that, aside from the global module space, you can't re-bind names in enclosed scopes; you can only have a local reference. An *ahem* unnamed coworker of mine who mainly uses Ruby seems to enjoy pointing this out. I actually like how you only get local refs in python; it seems safer. (As an aside I think Rite, Ruby2, is deprecating non-local refs by default but maybe that has changed.)

Python 3000 will solve this problem once and for all with the nonlocal keyword. Rejoice? I guess so. The PEP demonstrates the current workaround, a class they call Namespace, which—in the rare case that it's necessary—is how I do it and that never bothered me ... but doing without a workaround will certainly be nice.

I should also note that Python 2.5 added the with statement which provides a nice shortcut for transactional closures. This was spawned, I think, from a request to add ruby-style blocks to python.