3 ms·
Unfortunately in the example there is not a single closure shown. A closure is a function and its bindings. But there are no function objects created. What he
by lispm 16y ago
Unfortunately in the example there is not a single closure shown. A closure is a function and its bindings. But there are no function objects created.
What he shows is some attempt to describe some kind of lexical binding. But even that looks very suspicious.
def foo():
global y
x = 0
def bar():
print x, y
bar()
x = y = 1
bar()
x = y = 2
bar()
x = y = 3
bar()
x = y = 4
bar()
x = y = 5
foo()
0 0
1 1
2 2
3 3
4 4
Above is really trivial. The problem is that the assignments change a variable and the function BAR sees the changes. So there is only one variable binding for X. In a variant where each assignments were really bindings, BAR would only see the binding in its scope and the number would not change.
Above would be in Lisp:
(defvar *y* 0)
(defun foo ()
(let ((x 0))
(flet ((bar ()
(print (list x *y*))))
(bar)
(setf *y* 1 x *y*)
(bar)
(setf *y* 2 x *y*)
(bar)
(setf *y* 3 x *y*)
(bar)
(setf *y* 4 x *y*)
(bar)
(setf *y* 5 x *y*))))
Again, no closure. Just a function with a local function that gets called several times.
He then describes that in a 'subfunction' a variable from an outside scope can't be set, but that is almost a side issue. It also may be that this is not often found in his code, but it is found in a lot of Lisp code that actually uses closures.
The most trivial example is a counter closure:
(let ((fn (let ((x 0))
(lambda () (setf x (+ x 1))))))
(print (funcall fn))
(print (funcall fn))
(print (funcall fn)))
1
2
3
In above code FN gets bound to a closure. This closure is called three times. Each time it updates the variable X in its lexical scope.
- btilly 16y agoNo, they absolutely are full closures. He just didn't demonstrate that fact to your satisfaction. Here is a code example that may be more convincing to you. def make_counter(name): position = [0] def counter(): position[0] += 1 return (name, "incr", position[0]) def show_counter(): return (name, "show", position[0]) return counter, show_counter foo, see_foo = make_counter("foo") bar, see_bar = make_counter("bar") for i in range(5): print foo() print see_foo() for i in range(5): print bar() print see_bar() for i in range(5): print foo() print foo() print see_foo() print bar() print see_bar() If you run that you'll see that the returned closures retain lexical bindings to the scope they were defined in. Closures generated at different times do not interfere with each other. And closures generated in the same scope are pointing at the same variables. What more do you want?
- lispm 16y agoNothing. I never said that Python doesn't provide closures. I just pointed out that Mr. Prescod did not give an example for those, even though he writes about closures. He really was talking about lexical scope. He did not demonstrate closures for my satisfaction, he didn't demonstrate them at all. This makes me guess that Mr. Prescod never understood what a closure is, since he failed to give any example. That one can set closed over variables in Lisp is a useful feature. He demonstrates that Python 2 can't set a variable of an enclosing scope and claims that he does not need that. Maybe if he were really using closures, he would have found it useful.
- btilly 16y agoBut he did give an example. In his example, bar is a function object that is a closure. He just didn't make it do anything that would demonstrate it had to be a closure. As for Pascal, you need to look again. From the beginning Pascal had the ability to pass functions and procedures as parameters to other functions and procedures. If those functions and procedures are defined in an inner scope, they will carry the original lexical scope, and are in fact full closures. OK, they are clunky to use, but they work. Skip to the end of the section starting at http://en.wikipedia.org/wiki/Comparison_of_Pascal_and_C#Functions.2Fprocedures http://en.wikipedia.org/wiki/Comparison_of_Pascal_and_C#Func... to see the necessary syntax demonstrated. Don't quote me on this, but I believe that Pascal was actually the very first language with full closures. All of the necessary pieces are in ALGOL-68, but I think that Pascal was implemented first and so wins. Incidentally Sussman claims that he got the idea of lexical scope for Scheme from studying ALGOL. Of course Scheme realized the possibilities of the construct as no language before it had, and a lot of credit is due for that. But you shouldn't lightly dismiss the contributions to Lisp from languages out of the Lisp family.
- lispm 16y agoNo, bar is not a closure. It is basically just a local procedure. The original Pascal did not have full closures. Functions did only exist as long as their surrounding function was called. They could not be returned from functions. Pascal lacked also the necessary Garbage Collection. Pascal had lexical scope, but not full closures. All you could do was pass functions to other functions. But that's trivial, since each time one passed function X, it gets its active environment, which always is on the stack. There was no way to have closures, where functions have their own set of variable bindings, even when the surrounding function is no longer active. Being able to pass a function downwards is not sufficient to require full closures. Basically the same applies for Algol. Functions can't be returned and are not first class objects. What Algol contributed to the evolution of Lisp was lexical scope, but not closures. Also note that it was not Scheme that introduced closures in Lisp. That's what I fear, many people think they know what a closure is, but in reality there is a lot of confusion.
- zephyrfalcon 16y agoThe original post is from 2003. Much has changed since then. For example, in Python 3.x it's possible to write the "counter" example like this: def make_counter(): count = 0 def f(): nonlocal count count = count + 1 return count return f c = make_counter() for i in range(3): print(c()) (In Python 2.x and earlier, you would probably create a Counter class to do the same thing. That approach was, and likely still is, considered more "Pythonic", by the way.)
- lispm 16y agoI understand that. Still he said 'Python has closures' in the title and failed to give examples of closures.
- btilly 16y agoNo, 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.