On Mon, 10 Oct 2005, Steven D'Aprano wrote: > On Sun, 09 Oct 2005 17:39:23 +0200, Fredrik Lundh wrote: > >> only if you're obsessed with CPython implementation details. > > No. I'm obsessed with finding out what closures are, since nobody seems > to have a good definition of them!
On the contrary - the problem is that several people have good but incompatible definitions of them! I think you pretty much understand the mechanics of what's going on; i've spent god knows how long trying to write a clear but accurate definition of closures, but i can't, so i'm just going to say that (a) closures are functions, and (b) the things in func_closure are not closures - they're the variables over which a closure (the function you're inspecting) is closed; this is just sloppy terminology on the part of python's implementors. Okay, a crack at a definition: a closure is a function in which some of the variable names refer to variables outside the function. And i don't mean global variables - i mean somebody else's locals; call them 'remote variables'. The 'somebody else' who owns those locals is a function whose scope encloses the definition of the closure function. A crucial point is that these names keep working even after the scope where they started out dies - when a closure escapes the 'mother scope' that houses its remote variables, those variables effectively transcend, becoming, well, i don't know. Nonlocal variables or something. For some reason, this makes me think of Stephen Baxter novels. And a little of H. P. Lovecraft. I guess the point is that you shouldn't delve too deeply into the mysteries of functional programming if you wish to retain your humanity. tom -- Orange paint menace -- http://mail.python.org/mailman/listinfo/python-list