为什么说类是type的实例,但没有看到类本身继承type啊,我看到python安装包中typing.py有:
class Type(Generic[CT_co],extra=type):
"""A special construct usable to annotate class objects.
for example,suppose we have the following classes::
print(type(Person)) # 输出: <class 'type'>
所有类对象的本质都是type类的实例
这是Python面向对象模型的基石,称为"类也是对象"
不需要显式继承type,因为类的创建过程本身就是由type控制的
def create_instance(cls: Type[Person]) -> Person:
return cls()
typing.Type是类型提示(type hints)系统的组成部分
用于注解"接收类对象作为参数"的情况
与实际继承关系无关,是静态类型检查工具使用的元数据
from typing import Type
def get_class() -> Type[Animal]: # 注解:返回Animal类对象
return Animal
typing.Type的特殊性
python
class Type(Generic[CT_co], extra=type):
extra=type只是注解系统的实现细节
目的是让类型检查器理解这个注解与type的关系
不影响实际的类继承体系