为什么静态绑定对类和函数工作不同? [英] Why static binding works differently for class and function?

查看:163
本文介绍了为什么静态绑定对类和函数工作不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中(在2.7.6上测试),所有变量都是
在编译时静态绑定到范围。此过程很好,
http://www.python.org/ dev / peps / pep-0227 /
http:// docs.python.org/2.7/reference/executionmodel.html

In python (tested on 2.7.6) all variables are statically bound to a scope at compile time. This process is well described in http://www.python.org/dev/peps/pep-0227/ and http://docs.python.org/2.7/reference/executionmodel.html

明确说明如果名称绑定操作发生在
的任何地方一个代码块,所有使用的块内的名称
被视为对当前块的引用。

It is explicitly stated that "If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block."

函数是一个代码块,所以下面的代码失败,因为 x
在其使用后分配(因此在编译时定义local
,因为它被分配在函数中的某处,但是在执行
时,它在被绑定之前使用)。

A function is a code block so the following code with fail because x is assigned after its use (so at compile time it is defined local because it is assigned somewhere in the function, but at execution time, it is used before being bound).

x = 1
def f():
    print x 
    x = 2
    print x

>>> f()

Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    f()
  File "<pyshell#45>", line 2, in f
    print x
UnboundLocalError: local variable 'x' referenced before assignment

类也是一个代码块,所以我们应该完全遵守
的相同行为。但这不是我观察到的。
看这个例子:

A class is also a code block, so we should observe exactly the same behavior. But this is not what I observe. Look at this example:

x = 1
class C():
    y = x + 10
    x = 2
    def __init__(self):
        print C.y

>>> C.x
2
>>> C.y
11      
>>> C()
11
<__main__.C instance at 0x00000000027CC9C8>

由于类定义是一个代码块,所以在
块中的任何赋值应该变量局部。因此 x 应该位于
C 的本地,因此 y = x + 10 应导致 UnboundLocalError
为什么没有这样的错误?

As the class definition is a code block, any assignment within this block should make the variable local. So x should be local to the class C, so y = x + 10 should result in an UnboundLocalError. Why there is not such error?

推荐答案

是的 - 似乎文档是相当误导。类定义实际上不会像其他正常块一样工作:

Yes - it seems that the documentation is rather misleading. A class definition doesn't actually work quite the same as other normal blocks:

global_one = 0

class A(object):
    x = global_one + 10
    global_one = 100
    y = global_one + 20
    del global_one
    z = global_one + 30

a = A()
print a.x, a.y, a.z, global_one

10,120,30,0

如果您尝试使用同一个函数, c $ c> UnboundLocalError 在您首次访问 global_one 时出现。

if you try the same thing with a function, you get an UnboundLocalError on your first access of global_one.

这是类定义作为正常有权访问父作用域,但是,所有的名称赋值不会修改局部作用域,但实际上是捕获到类的数据属性字典。在文档中有关于这一点的提示,但它肯定不是很明显。

The reason for this is that class definitions as normal have access to the parent scope, however, all name assignments do NOT modify a local scope, but in fact are captured into the class's data attributes dictionary. There are hints about this in the documentation, but it's certainly not obvious.

这篇关于为什么静态绑定对类和函数工作不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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