源碼分析
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元類的使用,以及常用的場景。