2 ms·
No, he gave examples of things that really are closures, but failed to test them in ways that would have demonstrated that they in fact are closures.
by btilly 16y ago
No, he gave examples of things that really are closures, but failed to test them in ways that would have demonstrated that they in fact are closures.
- lispm 16y agoNo, closures are created when functions are passed or returned and there are free variables used in such a function. He did neither pass a function nor return one. The function itself is not a closure. The closure is an object that bundles a function and an actual set of bindings.
- omaranto 16y agoIn some languages, when you neither pass nor return a function a closure is not created. You can regard this as an optimization; in Python the closure is always created as far as I understand.
- lispm 16y agoThat looks more like a choice of some implementation and hopefully not of the language. I would hope for 'Python' that a Python compiler doesn't do that. There is no need to create a closure, when it is never needed. Requiring that for each call of a function with some local functions, closures are created for those, has little to do with 'we have closures', but more with a silly implementation.
- omaranto 16y agoI don't know if any python implementations avoid creating unnecessary closures, but you are right that I meant a specific implementation: CPython. I am not too familiar with python in the present and maybe the following has changed: there isn't really a definition of the language, but the documentation (which was written for the main implementation, but is the closest thing to a definition of the language) does mention that declaring a function creates a function object (closure) and assigns it to a local variable. Of course, an implementation should probably feel free to contradict the documentation when the difference is not observable.