Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between closures in python and javascript

In JS, we can write closure like:

function f(){
var a=0;
function g(){
    alert(a++);
}
return g;
}
g=f()
g()

However, if I write following code in python

def f():
    a=0
    def g():
        a+=1
        print a
    return g
g=f()
g()

Then I get UnboundedLocalError.

Can anyone tell me the difference between closure in python and JS?

like image 980
user607722 Avatar asked May 06 '11 10:05

user607722


People also ask

Does Python have closures like JavaScript?

Python closuresA closure is a nested function which has access to a free variable from an enclosing function that has finished its execution. Three characteristics of a Python closure are: it is a nested function. it has access to a free variable in outer scope.

Are there closures in Python?

Python Decorators make an extensive use of closures as well. On a concluding note, it is good to point out that the values that get enclosed in the closure function can be found out. All function objects have a __closure__ attribute that returns a tuple of cell objects if it is a closure function.

Are there closures in JavaScript?

In JavaScript, closures are created every time a function is created, at function creation time.

What is the difference between closures and decorators in Python?

A decorator is a function that takes in a function and returns an augmented copy of that function. When writing closures and decorators, you must keep the scope of each function in mind. In Python, functions define scope. Closures have access to the scope of the function that returns them; the decorator's scope.


2 Answers

When you use a += 1 in Python it refers to a local (uninitialized) variable in scope of g function. Basically you can read variables from upper scopes, but if you try to write it will refer to a variable in most recent scope. To make it work like you want you have to use nonlocal keyword that is only present Python 3. In Python 2 you can't do that as far as I know, unless the variable you're trying to change is is global, then global keyword comes to the rescue.

def f():
    a=0
    def g():
        nonlocal a
        a+=1
        print a
    return g
g=f()
g()
like image 75
Marek Sapota Avatar answered Sep 26 '22 09:09

Marek Sapota


Version for python 2:

def f():
    a=0
    def g():
        g.a+=1
        print g.a

    g.a=a
    return g
g=f()
g()
like image 26
freegnu Avatar answered Sep 22 '22 09:09

freegnu