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

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

问题描述

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

A 类(对象):打印你好"

出:

你好

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

解决方案

Everything 在 Python 第一次导入模块时在模块级别执行.函数体(和生成器表达式体)是这里的例外,而不是规则.Python 执行一切以创建包含在模块中的对象;就像 Python 中的一切一样,类是对象,函数也是.

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

所以函数和生成器表达式是例外,因为它们的全部目的是在稍后执行.注意函数定义 执行的:

<预><代码>>>>导入文件>>>dis.dis(compile('def foo(): pass', '', 'exec'))1 0 LOAD_CONST 0(<0x106aef2b0 处的代码对象 foo,文件",第 1 行>)3 MAKE_FUNCTION 06 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天全站免登陆