不 - 的__class__
属性是在 C API 级别本身“看到”的所有 Python 对象布局的基本信息。这就是通过调用来检查的type
.
这意味着:每个 Python 对象在其内存布局中都有一个槽,其中包含用于指向 Python 对象(即该对象的类)的单个指针的空间。
即使您使用 ctypes 或其他方法来覆盖对该插槽的保护并从 Python 代码中更改它(因为修改obj.__class__
with =
在 C 级别受到保护),更改它会有效地更改对象类型:__class__
slot 是对象的类,并且test
方法将从示例中的类(Bar)中选取。
但是这里有更多信息:在所有文档中,type(obj)
被视为等价于obj.__class__
- 但是,如果对象的类定义了一个具有名称的描述符__class__
,当使用以下形式时使用obj.__class__
. type(obj)
但是会检查实例的__class__
直接插入并返回真实的类。
所以,这可以“欺骗”代码使用obj.__class__
, 但不是type(obj)
:
class Bar:
def test(self):
return 2
class Foo:
def test(self):
return 1
@property
def __class__(self):
return Bar
元类的属性
试图搞乱创建一个__class__
元类上的描述符Foo
本身就会很混乱——两者type(Foo())
and repr(Foo())
将报告一个instance of Bar
,但“真正的”对象类将是 Foo。从某种意义上说,是的,它使得type(Foo())
谎言,但不是你想象的那样 - type(Foo()) 将输出以下的 reprBar()
, 但它是Foo
由于内部的实现细节, 的 repr 被搞乱了type.__call__
:
In [73]: class M(type):
...: @property
...: def __class__(cls):
...: return Bar
...:
In [74]: class Foo(metaclass=M):
...: def test(self):
...: return 1
...:
In [75]: type(Foo())
Out[75]: <__main__.Bar at 0x55665b000578>
In [76]: type(Foo()) is Bar
Out[76]: False
In [77]: type(Foo()) is Foo
Out[77]: True
In [78]: Foo
Out[78]: <__main__.Bar at 0x55665b000578>
In [79]: Foo().test()
Out[79]: 1
In [80]: Bar().test()
Out[80]: 2
In [81]: type(Foo())().test()
Out[81]: 1
修改type
itself
既然没有人“进口”type
从任何地方,只需使用
内置类型本身,可以对内置类型进行猴子修补type
可调用来报告错误的类 - 它适用于所有人
Python代码在同一进程中依赖调用type
:
original_type = __builtins__["type"] if isinstance("__builtins__", dict) else __builtins__.type
def type(obj_or_name, bases=None, attrs=None, **kwargs):
if bases is not None:
return original_type(obj_or_name, bases, attrs, **kwargs)
if hasattr(obj_or_name, "__fakeclass__"):
return getattr(obj_or_name, "__fakeclass__")
return original_type(obj_or_name)
if isinstance(__builtins__, dict):
__builtins__["type"] = type
else:
__builtins__.type = type
del type
这里有一个技巧我在扩展坞中没有找到:访问时__builtins__
在程序中,它充当字典。然而,在Python的Repl或Ipython等交互式环境中,它是一个
模块 - 检索原始数据type
并写出修改后的
版本为__builtins__
必须考虑到这一点 - 上面的代码
双向工作。
并对此进行测试(我从磁盘上的 .py 文件导入了上面的代码片段):
>>> class Bar:
... def test(self):
... return 2
...
>>> class Foo:
... def test(self):
... return 1
... __fakeclass__ = Bar
...
>>> type(Foo())
<class '__main__.Bar'>
>>>
>>> Foo().__class__
<class '__main__.Foo'>
>>> Foo().test()
1
尽管这适用于演示目的,但替换内置类型会导致“不和谐”,这在更复杂的环境(例如 IPython)中被证明是致命的:如果运行上面的代码片段,Ipython 将崩溃并立即终止。