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

标签 python compiler-construction cell free abstract-syntax-tree

python 依赖于 cell 中的 __class__ 变量来进行 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__)

本地人:

            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__ cell 是 Python 3 中的一个 hack,允许 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 的函数时,单元格才存在不带参数调用(实际上不必是 super,例如 super = print; super() 会欺骗编译器创建 __class__ 单元)或者 if __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]

关于python - `__class__`变量存储在python中的哪里,或者编译器如何知道在哪里找到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45522210/

相关文章:

python - 一个 while 循环来向上和向下计数

java - 找不到标志

tableview - Xcode 6 + Swift 中的自定义单元格不显示

ios - Swift:根据其中的 UICollectionView 的大小扩展 UITableViewCell 高度

javascript - JS : Is there a way to make a cell in a grid not clickable?

python - 返回外部函数错误

python - 使用 Python 类中的方法作为 PySpark 用户定义函数

python - 为什么我会收到 TypeError : get() takes exactly 2 arguments (1 given)? Google App Engine

compiler-construction - GNAT 支持的目标架构列表?

oop - 为什么几乎所有的面向对象语言都编译为字节码?