在Python本地功能 [英] Local functions in Python

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

问题描述

在下面的Python code,我得到一个 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()

这似乎是内在功能已收到所有引用的副本在父功能,因为我没有得到 UnboundLocalError 异常,如果<$ c的值$ C> A 被包裹在一个非不变型。

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 non-immutable type.

时有人能够在这里澄清的行为,并指出我在这个适当的Python文档?

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

推荐答案

我相信你在看到这是一个可变性的问题是正确的。当您发布的code确实抛出一个UnboundLocalError,下面code不:

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没有让你从外部范围重新分配在内部范围变量的值(除非你使用关键词全球性,这并不适用于这种情况)。

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:

[&hellip;如果一个名字被宣布全球性的,那么所有的引用和分配去
  直接到包含模块的全局名字中间的范围。
  否则,最里面的范围之外发现的所有变量
  只读(尝试写这样的变量将只需创建一个
  在最里面的范围,新的局部变量,留下相同
  名为外部变量不变)。

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 + = 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通过可变类型的内容(如你可能已经注意到在测试这种设置)。

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天全站免登陆