您的位置:首页 > 编程语言 > Python开发

Python中type和object类的关系

2021-07-19 22:16 1091 查看

源码分析

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元类的使用,以及常用的场景。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: