Python中type和object类的关系

源码分析

class type(object):
    """
    type(object_or_name, bases, dict)
    type(object) -> the object's type
    type(name, bases, dict) -> a new type
    """
    pass
 
class object:
    """
    The base class of the class hierarchy.
    
    When called, it accepts no arguments and returns a new featureless
    instance that has no instance attributes and cannot be given any.
    """
    pass
  

可以简单的看得,object是type的父类,那么type是继承object基类的。

简单的输出

print(type(type))
print(type(object))

# 输出结果
# <class 'type'>
# <class 'type'>

那么说明type其实是类型的顶端,而object是类的顶端。

总结

  • type类是数据类型的顶端,我们除了object的type也是type。
  • type类的父类是object,那么说明object类是继承类的顶端。
  • 构造数据类型需要使用到type类,那么如果我们想创建自己的自定义类就可以继承type实现创建自己的自定义类型,同时可以使用很多魔方方法来实现自己的类型的内容的封装。
  • 以后机会详细讲解一下type元类的使用,以及常用的场景。
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容