3 ms·
In 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
by omaranto 16y ago
In 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.