python: how binding works

前端 未结 5 1603
难免孤独
难免孤独 2021-01-04 13:26

I am trying to understand, how exactly variable binding in python works. Let\'s look at this:

def foo(x):
    def bar():
        print y
    return bar

y =          


        
相关标签:
5条回答
  • 2021-01-04 14:03

    The second example implements what is called a closure. The function bar is referencing the variable x from its surrounding context, i.e. the function foo. This precedes the reference to the global variable x.

    See also this question Can you explain closures (as they relate to Python)?

    0 讨论(0)
  • 2021-01-04 14:14

    The issue that you are alluding to is one of lexical vs dynamic scoping of variables in python. To be explicit, python defines the following four scopes.

    1. The innermost scope, which is searched first, contains the local names
    2. The scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names
    3. the next-to-last scope contains the current module’s global names
    4. the outermost scope (searched last) is the namespace containing built-in names

    In the first example, where "y" is defined outside of the function bar, python searched for the innermost scope and moved up the chain until it found the global variable "y" in the module

    In the second example, where "x" is being defined by function foo(x), x belongs to the scope of an enclosing function, when its being printed inside bar. In pure terms, a closure has been defined.

    For further study on scoping in python, I found the following article a great read

    0 讨论(0)
  • 2021-01-04 14:19

    Nothing strange, it is because "x" from function argument has higher priority than global variable "x".

    At first, global variables is a great evil.

    Python has operator "global":

    >>> def foo(x):
    ...     def bar():
    ...          global x
    ...          print x
    ...     return bar
    ... 
    >>> x = 5
    >>> bar = foo(2)
    >>> bar()
    5
    
    0 讨论(0)
  • 2021-01-04 14:23

    It is mater of scope, second example uses local scope variable x, that is preceded above globally declared

    0 讨论(0)
  • 2021-01-04 14:26

    In both examples, the lookup happens at runtime. The only difference is that there's a locally defined variable x, while there isn't a locally defined variable y.

    When executing ...

    def foo(x):
        def bar():
            print y
    
        return bar
    
    y = 5
    bar = foo(2)
    bar()
    

    ... the print statement looks up the variable named y, and only finds it in the global context, so it uses that one, and prints "5".

    In ...

    def foo(x):
        def bar():
            print x
    
        return bar
    
    x = 5
    bar = foo(2)
    bar()
    

    ... when the lookup occurs, there is a scoped variable x defined--which is fixed at "5" when the foo function is called.

    The key is that arguments are evaluated at the time they're passed into functions, so the outer function foo evaluates the arguments passed in when it's called. This effectively creates a variable called x in the context of the foo function, so whenever bar executes it sees that variable, and not the globally defined one.

    This can occasionally be confusing, as in the following code:

    lst = []
    for i in range(5):
        x = i
        lst.append(lambda: x)
    
    for func in lst:
        print func()  # prints 4 4 4 4 4
    

    You need to do:

    lst = []
    for i in range(5):
        def _func(x):
            return lambda: x
    
        lst.append(_func(i))
    
    for func in lst:
        print func()  # prints 0 1 2 3 4
    
    0 讨论(0)
提交回复
热议问题