如何创建一个不是类型实例的 class?
How to create a class that is not an instance of type?
根据3.3.3.2. Resolving MRO entries:
If a base that appears in class definition is not an instance of type,
then an _mro_entries_ method is searched on it.
所以,我想 应该 是一种创建 class 的方法,它 而不是 类型。但是怎么办?
我在Python中只知道两种创建class的方法:
- A class 定义 (
class MyClass(...): ...
) 创建 type 的实例或 [=22= 给出的给定 metaclass ]metaclass 论点(实际上,是从包括碱基的 metaclasses 在内的集合中派生得最多的一个)。
- 调用 type(name, bases, dict) 创建 type.
的实例
还有其他办法吗?如何创建不是类型实例的 class?
哦,我想我明白了!
有 no 方法来创建 class 不是 type[ 的实例=29=].
但基数不必是 class - 令人惊讶的是,但有可能。
所以,实际上 Python 文档中的句子应该读作:
If a base is not a class, then an _mro_entries_ method is searched
on it.
事实上,在 PEP 560 中它说:
If an object that is not a class object appears in the tuple of bases
of a class definition, then method _mro_entries_ is searched on it.
根据3.3.3.2. Resolving MRO entries:
If a base that appears in class definition is not an instance of type, then an _mro_entries_ method is searched on it.
所以,我想 应该 是一种创建 class 的方法,它 而不是 类型。但是怎么办?
我在Python中只知道两种创建class的方法:
- A class 定义 (
class MyClass(...): ...
) 创建 type 的实例或 [=22= 给出的给定 metaclass ]metaclass 论点(实际上,是从包括碱基的 metaclasses 在内的集合中派生得最多的一个)。 - 调用 type(name, bases, dict) 创建 type. 的实例
还有其他办法吗?如何创建不是类型实例的 class?
哦,我想我明白了!
有 no 方法来创建 class 不是 type[ 的实例=29=].
但基数不必是 class - 令人惊讶的是,但有可能。
所以,实际上 Python 文档中的句子应该读作:
If a base is not a class, then an _mro_entries_ method is searched on it.
事实上,在 PEP 560 中它说:
If an object that is not a class object appears in the tuple of bases of a class definition, then method _mro_entries_ is searched on it.