Why isn't Python lexical scoping?

Source: Internet
Author: User
This seems to deviate from the general design idea of modern programming language.

Like what

def Foo ():    x = 1    def Inner ():        return x + 1    x = 3    Print Inner ()

Reply content:

This code is lexical scoping, the static scope is that we can determine the existence of a variable based on the location of the code text.

In accordance with Python's LEGB (local,enclosing,global,built-in) rule, when calling inner (), X is actually found in the scope of Foo. inner can access the X in Foo because inner is inside the text of Foo, which is exactly what lexical means

Bash is the dynamic scoping
x=1                                                                                                 function(){echo$x;x=2;}function(){local x=3;;} f  #f中的g执行时打印出的x是3而不是1echo$x  #这时打印出的x是1
Do you think Python is

letfoo()=  letx=1in    letinner()=x+1in      letx=3in        print(inner())
The free variables in a python's closure are passed by reference, not by value, so there is this result.
It has nothing to do with scoping.
C + + lambda can choose capture by copy or capture by reference. Based on the previous experience of reading Python's source code (if mistaken), in the case of the master, the inner is a closure. The way closures are implemented in Python is to save a pointer to an external namespace (which can be understood as a dictionary).

Landlord can see this example
deffoo():definner():returnx+1x=1printinner()# output 2x=2printinner()# output 3

This is clearly lexical scoping, for example, the equivalent C # code


voidFoo(){    intx=1;    Func<int>inner=()=>x+1;    x=3;    Console.WriteLine(inner());}
Rewrite the code of the landlord to Lua to see how Python and Lua are handled differently:

The first example turns out to be the same, because a variable binds a reference instead of a value. A second example:

functionfoo()functioninner()returnx+1endlocalx=3print(inner())endfoo()
What is the modern programming language referred to by the main question? JS after so many generations of update iterations, and now is the same ~

 ( function  foo   ()  { function  inner   ()  { return   x  +  1  ;  }  x  =  3    console   log   ( inner   ());    
deffoo():    x=1    definner():        returnx+1    x=3    printinner()foo()  # 输出4
I don't think it's quite a departure.
Output feel the current x go this question is very good answer: X is the inner of the function, so in the definition of inner x is actually a reference to the outside of the definition, that is, the inner of the environment.

The function is only executed when it is tuned, you will have 1 in front and the back is changed to 3, but it changes the reference value of x, which is the value for x. Then execute the inner function, using the value of X as 3, so the answer is 4.
  • 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.