python 依赖于 cell
中的 __class__
变量来进行 super()
调用。它从第一个堆栈帧中的free
变量获取此单元格。
奇怪的是,这个变量不在 locals()
中,而是在您从 __init__
方法中引用它时出现。
以这段代码为例:
class LogicGate:
def __init__(self,n):
print(locals())
a = __class__
print(locals())
当您反汇编它时,您可以看到它以某种方式知道 print
和 locals
是全局变量,而 __class__
是 LOAD_DEREF
.在运行代码之前,编译器如何知道这一点。据我所知,locals
、print
和__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/