Comparison of closures in Python and JavaScript

Source: Internet
Author: User
As with scripting languages, Python and JavaScript have similar variable scopes, unlike PHP, where all the variables inside the function are isolated from the outside, that is, the function wants to work with its external data, The data that needs to be processed must be passed in using parameters (not discussed here using Global keywords), and unlike Python and JavaScript, if a variable is declared in a function, it is searched online until it returns a value or is undefined.

So, Python's closures should be simple, like JavaScript, where we write code like this:

Def func1 ():    a = 1    def func2 ():        a = a + 1        return a    return func2re=func1 () print re () print re ()


However, the reality is that the results did not appear as we expected to print out 2 and 3, instead there was an error: "Unboundlocalerror:local variable ' a ' referenced before assignment" ( Local variable A is referred to before the value is assigned. Why this problem occurs, we first look at the JS is if the implementation of this closure:

The above code runs as expected, entering 2 and 3. Notice the 5th line of the program, if I add a var in front of it, what is the result of this program running? The end result is that two "NaN" was entered and a description of Var was found on the Firefox developer platform:

Declares a variable, optionally initializing it to a value.
The scope of a variable declared with VAR are the enclosing function or, for variables declared outside a function, the Glo Bal scope (which is bound to the global object).

This means that Var is used to declare local variables, in the above example, if you use Var a=a+1, then A is already a local variable in the FUNC2, but not inherit from the func1, so the result of Nan will appear finally.

Let's go back to this closure of Python, which means that a is a local variable, and in fact Python specifies that all variables on the left side of the assignment statement are local variables, and that a is to the left of the equals sign, so it becomes a local variable, causing me to not access a in func1. How can we solve this problem? If it is above python3.0, before a=a+1, you can use nonloacal A to specify that a is not a local variable. The Nonloacal keyword is not supported in versions below 3.0 and we can do this:

Def func1 ():    a = [1]    def func2 ():        a[0] = a[0] + 1        return a[0]    return func2re=func1 () print re () print re ()

The results, as we expected, were 2 and 3 printed. From the examples of Python and JavaScript closures, learn about Python and JS variable declarations, the similarity and difference of variable scopes.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.