Python的非本地性取决于层次结构级别? [英] Python's nonlocal depends on level of hierarchy?

查看:93
本文介绍了Python的非本地性取决于层次结构级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是有关有关Python变量范围的问题的后续内容。其他问题 q1 q2 答案可以在SO上找到,甚至更多。
Python文档 PEP 3104 来解释细节,但是他们没有

This question is a follow-up on a question about Python variable scope. Additional questions q1, q2 and answers can be found on SO, among even more. The official Python documentation, and PEP 3104 are supposed to explain the details, but they don't seem fully self-explanatory to me.

我要解决的主题是重构包含 nonlocal / global ,将代码上移/下移一层。

The topic I'm trying to solve is refactoring of code containing nonlocal/global by moving that code up/down one level of hierarchy.

我不了解的是这句话来自Python参考文献的含义:

What I do not understand are the implications of this sentence from the Python reference:


在非本地语句中列出的名称与在
全局语句中列出的名称不同,必须引用封闭的
范围中的先前存在的绑定(不能明确确定要在其中创建新绑定的范围)。

Names listed in a nonlocal statement, unlike to those listed in a global statement, must refer to pre-existing bindings in an enclosing scope (the scope in which a new binding should be created cannot be determined unambiguously).

给出以下有关全局范围的代码:

Given the following code on global scope:

var = 0
def outer():
    global var                  # line A
    var = 1
    def inner():
        nonlocal var            # line B
        var = 2

        print('inner:', var)

    inner()
    print('outer:', var)

outer()
print('main:', var)

执行会引发错误:

SyntaxError: no binding for nonlocal 'var' found

代码可以正常工作(具有不同的语义,当然,如果任一行A被注释掉:

The code works (with different semantics, of course, if either line A is commented out:

inner: 2
outer: 2
main: 0

或注释掉了行B:

inner: 2
outer: 1
main: 1

但是,在上面的示例,并且由于 nonlocal 应该将var绑定到封闭范围,所以我希望A行将外部/ var绑定到全局范围中, d行B然后寻找external / var,并且还将inner / var重新绑定到global / var。相反,它似乎根本找不到(由于我认为由于A行中的重新绑定)并引发错误。

However, in the above example, and since nonlocal is supposed to bind var to the "enclosing scope", I would have expected that line A binds the outer/var into global scope and line B then looks for outer/var and also rebinds inner/var to global/var. Instead it seems to not find it at all (due to the rebinding in line A, I suppose) and raise an error.

我期望的预期结果是:

The desired result I expected was:

inner: 2
outer: 2
main: 2

这仅仅是Python范围界定令人困惑的心态的又一个例子吗?

Is this just one more example of the confusing state of mind of scoping in Python?

或者,要使它成为一个建设性的问题:

Or, to make this a constructive question:


  • 如何编写这样的示例,而无论在哪个级别上函数存在(必须与 nonlocal 交换 global ,反之亦然)?

  • 如果函数位于中间的,未知的层次结构中, outer()的作者怎么能更改最外层(在这种情况下为全局层)的代码,也不必触及 inner()级别吗? -

  • How can such an example be written in a way that it does not matter at which level a function resides (having to exchange global with nonlocal and vice versa)?
  • If the functions reside at an intermediate, and unknown level of hierarchy, how could the author of outer() change the code that neither the outermost (in this case global) level, nor the inner() level have to be touched? -

在我对语言的拙劣理解中,应避免使用此类结构(依赖于闭包)。其他人已经建议使用其他语言功能(func attrs )来实现这种上下文敏感性。

In my humble understanding of the language, constructs like these (dependecies on closures) are just to be avoided. Others already have suggested to use other language features (classes, func attrs) to achieve this kind of context sensitivity.

推荐答案

global nonlocal 并不意味着结合。它们的含义不同:

global and nonlocal are not meant to be combined. They mean different things:


  • global 表示名称存在于模块级别

  • nonlocal 表示该名称存在于外部词法功能范围内

  • global means the name exists at the module level
  • nonlocal means the name exists in an outer lexical function scope

得到原始异常的原因是因为您告诉Python var 是非本地的(这意味着它在外部函数定义中),但是在那里在任何外部函数定义中对于 var 没有函数级绑定,因为您在外部函数中告诉Python var 是全球性的。

The reason you are getting the original exception is because you told Python that var is nonlocal (meaning it is in an outer function definition), but there is no function-level binding for var in any outer function definition because you told Python in the outer function that var was global.

这篇关于Python的非本地性取决于层次结构级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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