The closures in Python and JavaScript are more _python

Source: Internet
Author: User
Tags closure

As a scripting language, 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 process its external data, The data that needs to be processed must be passed in using parameters (not discussed here using Global keywords), and Python and JavaScript are different, and if you declare a variable in a function, it is searched over the web until it returns a value or is undefined.

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

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


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

<script>
 function Func1 () {
 var a=1;
  function Func2 () {
  a=a+1;
  return A;
  }
 return func2;
 }
Re=func1 ();
Console.log (re ());
Console.log (re ());
</script>

The results of the above code are as we expected, input 2 and 3. Notice the 5th line of the program, and if I add a var in front of it, what is the result of the program running? The end result was a two "NaN", 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 is 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 a local variable, and in the above example, if you use Var a=a+1, then A is already a local variable in FUNC2 and not inherited from the func1, so the result of Nan will eventually appear.

Let's go back to the Python closure, the meaning of this error hint is also that a is a local variable, in fact, Python stipulates that all variables on the left side of the assignment statement are local variables, which is the left of the equals sign, so it becomes a local variable, which causes me to not access the A in func1. How can we solve this problem? If you are above python3.0, you can use nonloacal A to specify that a is not a local variable before a=a+1. 3.0 The following version does not support the Nonloacal keyword, we can do this:

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

The results of the operation, as we expected, printed out 2 and 3. From examples of Python and JavaScript closures, understand the similarities and differences between Python and JS variable declarations, variable scopes.

Related Article

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.