为什么类的主体在定义时执行? [英] Why does a class' body get executed at definition time?

查看:221
本文介绍了为什么类的主体在定义时执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与函数相反,类的主体在定义时执行:

 类A $ b print'hello'

出:

  hello 

为什么会这样?是与 @classmethod / @staticmethod 方法和类属性相关吗?

> >。函数体(和生成器表达式主体)是这里的例外,而不是规则。 Python执行一切来创建模块中包含的对象;如同Python中的一切,类是对象,函数也是。



类体使用单独的代码对象的唯一原因是类体在一个单独的命名空间,然后用该命名空间形成类属性。类体并不是唯一的这样的命名空间; set和dict comprehensions,在Python 3中,列表推导也是用一个单独的命名空间执行的,它们定义了它们的局部变量。



所以函数和生成器表达式是例外,他们的整个目的 将在以后执行。请注意,执行的功能

  > import dis 
>>>> dis.dis(compile('def foo():pass','< stdin>','exec'))
1 0 LOAD_CONST 0(< code object foo at 0x106aef2b0,file< stdin> ,line 1>)
3 MAKE_FUNCTION 0
6 STORE_NAME 0(foo)
9 LOAD_CONST 1(无)
12 RETURN_VALUE

MAKE_FUNCTION 字节码会创建函数对象以及该函数的存储字节码,结果绑定到全局名 foo



类对象在这里没有什么不同; class 语句生成一个类对象,作为该对象的一部分,我们需要知道类体中的属性。



如果Python不是执行类体,其他代码不能使用这些类成员。您无法访问类属性(包括类方法和静态方法),您无法设置类属性等。



任何函数作为类体的一部分当然是 当时执行。与顶层函数一样,只执行一个 MAKE_FUNCTION 字节码,生成的本地名称(设置为 STORE_FAST )然后转换为类属性,类似于全局函数对象绑定到全局 STORE_NAME


In contrast to functions, a class' body is executed at definition time:

class A(object):
    print 'hello'

Out:

hello

Why is it the case? Is it related to @classmethod / @staticmethod methods and class attributes?

解决方案

Everything is executed at the module level when Python first imports a module. Function bodies (and generator expression bodies) are the exception here, not the rule. Python executes everything to create the objects contained in a module; like everything in Python, classes are objects, and so are functions.

The only reason a class body uses a separate code object is because a class body is executed in a separate namespace, with that namespace then forming the class attributes. Class bodies are not the only such namespaces; set and dict comprehensions, and in Python 3, list comprehensions are also executed with a separate namespace, scoping their locals.

So functions and generator expressions are the exception, expressly because their whole purpose is to be executed at a later time. Note that the function definition is executed:

>>> import dis
>>> dis.dis(compile('def foo(): pass', '<stdin>', 'exec'))
  1           0 LOAD_CONST               0 (<code object foo at 0x106aef2b0, file "<stdin>", line 1>)
              3 MAKE_FUNCTION            0
              6 STORE_NAME               0 (foo)
              9 LOAD_CONST               1 (None)
             12 RETURN_VALUE        

The MAKE_FUNCTION bytecode there creates the function object, together with the stored bytecode for that function, and the result is bound to the global name foo.

Class objects are no different here; the class statement produces a class object, and as part of that object, we need to know the attributes from the class body.

If Python did not execute the class body, other code could not make any use of those class members. You couldn't access class attributes (including class methods and static methods), you couldn't set class attributes, etc.

Any functions that are part of the class body are of course not executed at that time. Just like top-level functions, only a MAKE_FUNCTION bytecode is executed and the resulting local name (set with STORE_FAST) is then turned into a class attribute, analogous to a global function object being bound to a global with STORE_NAME.

这篇关于为什么类的主体在定义时执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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