Why nested functions can access variables from out

2019-01-21 18:16发布

This question already has an answer here:

In the 2nd case below, Python tries to look for a local variable. When it doesn't find one, why can't it look in the outer scope like it does for the 1st case?

This looks for x in the local scope, then outer scope:

def f1():
    x = 5
    def f2():
         print x

This gives local variable 'x' referenced before assignment error:

def f1():
    x = 5
    def f2():
        x+=1

I am not allowed to modify the signature of function f2() so I can not pass and return values of x. However, I do need a way to modify x. Is there a way to explicitly tell Python to look for a variable name in the outer scope (something similar to the global keyword)?

Python version: 2.7

标签: python scope
2条回答
可以哭但决不认输i
2楼-- · 2019-01-21 19:08

In Python 3.x this is possible:

def f1():
        x = 5
        def f2():
                nonlocal x
                x+=1
        return f2

The problem and a solution to it, for Python 2.x as well, are given in this post. Additionally, please read PEP 3104 for more information on this subject.

查看更多
相关推荐>>
3楼-- · 2019-01-21 19:11
def f1():
    x = { 'value': 5 }
    def f2():
        x['value'] += 1

Workaround is to use a mutable object and update members of that object. Name binding is tricky in Python, sometimes.

查看更多
登录 后发表回答