Python 中的局部函数 [英] Local functions in Python

查看:20
本文介绍了Python 中的局部函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的 Python 代码中,我得到一个 UnboundLocalError.据我了解,局部函数共享包含函数的局部变量,但这里的情况似乎并非如此.我认识到 a 在这种情况下是一个不可变的值,但这应该不是问题.

In the following Python code, I get an UnboundLocalError. As I understand it, local functions share the local variables of the containing function, but this hardly seems to be the case here. I recognise that a is an immutable value in this context, but that should not be a problem.

def outer():
    a = 0
    def inner():
        a += 1
    inner()
outer()

看起来内部函数已经收到了父函数中所有引用的副本,因为如果 a 的值是,我不会收到 UnboundLocalError 异常包裹在可变类型中.

It would seem that the inner function has received copies of all the references in the parent function, as I do not get the UnboundLocalError exception if the value of a is wrapped in a mutable type.

有人能够澄清这里的行为,并指出有关此问题的相应 Python 文档吗?

Is someone able to clarify the behaviour here, and point me to the appropriate Python documentation on this?

推荐答案

我相信您将其视为可变性"问题是正确的.虽然您发布的代码确实会引发UnboundLocalError",但以下代码不会:

I believe you're correct in seeing this as a "mutability" problem. While the code you posted does throw an "UnboundLocalError", the following code does not:

def outer():
    a = 0
    def inner():
        print a
    inner()
outer()

Python 不允许您从内部作用域的外部作用域重新分配变量的值(除非您使用关键字global",这在本例中不适用).

Python doesn't allow you to reassign the value of a variable from an outer scope in an inner scope (unless you're using the keyword "global", which doesn't apply in this case).

查看此 Python 2.6.2 文档中类"文档的底部部分:

Check out the bottom section of the "classes" documentation in this Python 2.6.2 documentation:

[…] 如果一个名字被声明为全局的,那么所有的引用和赋值都会被直接到包含模块全局名称的中间范围.否则,在最内部范围之外找到的所有变量都是只读(尝试写入这样的变量只会创建一个最内层作用域中的新局部变量,保留相同的命名外部变量不变).

9.2. Python Scopes and Namespaces

[…] If a name is declared global, then all references and assignments go directly to the middle scope containing the module’s global names. Otherwise, all variables found outside of the innermost scope are read-only (an attempt to write to such a variable will simply create a new local variable in the innermost scope, leaving the identically named outer variable unchanged).

您的UnboundLocalError"是因为您的函数实际上声明了一个名为a"的新变量,然后立即尝试对其执行+="操作,但这失败了,因为a"还没有值.(将a+=1"视为a = a+1",如果a"未定义,您可以看到问题).

Your "UnboundLocalError" is because your function is actually declaring a new variable called "a" and then immediately trying to do a "+=" operation on it, but this fails because "a" does not have a value yet. (View the "a+=1" as "a = a+1" and you can see the problem if "a" is undefined).

一般来说,如果您要修改a",人们通常绕过它的方法是使用可变类型来传递a"(例如列表或字典).您可以通过可变类型的内容修改a"(正如您在使用此设置进行的测试中可能注意到的那样).

In general, if you're going to want to modify "a", the way people usually get around it is to use a mutable type to pass "a" around (such as a list or a dictionary). You can modify "a" via the contents of the mutable type (as you probably noticed in your testing with this setup).

希望有帮助!

这篇关于Python 中的局部函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆