python 中存储的 `__class__` 变量在哪里,或者编译器如何知道在哪里找到它

where is the `__class__` variable stored in python, or how does the compiler know where to find it

python 依赖 __class__ 变量在 cell 中用于 super() 调用。它从第一个堆栈帧中的 free 变量中获取此单元格。

奇怪的是这个变量不在 locals() 中,而当你从 __init__ 方法中引用它时它就出现了。

以这段代码为例:

class LogicGate:
    def __init__(self,n):
        print(locals())
        a = __class__
        print(locals())

当你反汇编它时,你可以看到它以某种方式知道 printlocals 是全局变量,而 __class__LOAD_DEREF。在 运行 代码之前,编译器如何知道这一点。据我所知,localsprint__class__ 只是编译器的变量名。同样,__class__ 突然出现在 locals() 中,甚至在它被复制到 a.

之前
4          10 LOAD_DEREF               0 (__class__)

同时 locals

            2 LOAD_GLOBAL              1 (locals)

我问是因为我正在研究 python 到 javascript 的编译器。目前该编译器不区分 print__class__ 并试图从全局范围中获取它们。

正如您从上面代码的 ast 打印输出中看到的,解析器不区分 locals__class__:

Module(body=[ClassDef(name='LogicGate',
    bases=[],
    keywords=[],
    body=[FunctionDef(name='__init__',
        args=arguments(args=[arg(arg='self',
                                 annotation=None),
                             arg(arg='n',
                                  annotation=None)],
                       vararg=None,
                       kwonlyargs=[],
                       kw_defaults=[],
                       kwarg=None,
                       defaults=[]),
        body=[Expr(value=Call(func=Name(id='print',
                                        ctx=Load()),
                                              # here's the load for locals
                              args=[Call(func=Name(id='locals',
                                                   ctx=Load()),
                                         args=[],
                                         keywords=[])],
                              keywords=[])),
              Assign(targets=[Name(id='a',
                                   ctx=Store())],
                           # here's the load for __class__
                     value=Name(id='__class__',
                                ctx=Load())),
              Expr(value=Call(func=Name(id='print',
                                        ctx=Load()),
                              args=[Call(func=Name(id='locals',
                                                   ctx=Load()),
                                         args=[],
                                         keywords=[])],
                              keywords=[]))],
        decorator_list=[],
        returns=None)],
   decorator_list=[])])

__class__ 单元格是 Python 3 中的一个 hack,允许在没有 args 的情况下调用 super。在 Python 2 中,您必须使用样板参数调用 super(即。super(<current class>, self))。

__class__ 单元格本身存储在 <function>.__closure__ 元组中。 __class__ 单元格的索引可以通过在 <function>.__code__.co_freevars 元组中找到它的索引来获得。例如,

>>> class A:
    def __init__(self):
        super().__init__()

>>> A.__init__.__code__.co_freevars
('__class__',)
>>> A.__init__.__closure__
(<cell at 0x03EEFDF0: type object at 0x041613E0>,)
>>> A.__init__.__closure__[
        A.__init__.__code__.co_freevars.index('__class__')
    ].cell_contents
<class '__main__.A'>

但是,根据函数的不同,如果函数不使用单元格,co_freevars__closure__ 可能是 None。此外,不保证 __class__ 存在。 __class__ 单元仅在调用名为 super 的函数时不带 args 时才会出现(实际上不必是超级函数,例如 super = print; super() 会欺骗编译器创建 __class__ 单元格)或者如果 __class__ 被显式引用并且不是本地的。您也不能假定 __class__ 单元格始终位于索引 0 处,如以下(尽管很奇怪)代码所示:

class A:
    def greet(self, person):
        print('hello', person)

def create_B(___person__):
    class B(A):
        def greet(self):
            super().greet(___person__)
    return B

B = create_B('bob')
B().greet() # prints hello bob

assert [c.cell_contents for c in B.greet.__closure__] == ['bob', B]