溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

詳解Python中的type和object

發(fā)布時間:2020-08-31 20:56:50 來源:腳本之家 閱讀:166 作者:Harvard_Fly 欄目:開發(fā)技術

type  所有類是type生成的

a = 1
b = "abc"
print("type a:{}".format(type(a)))
print("type int:{}".format(type(int)))
print("type b:{}".format(type(b)))
print("type str:{}".format(type(str)))

result:

type a:<class 'int'>
type int:<class 'type'>
type b:<class 'str'>
type str:<class 'type'>

在python中是一切皆對象的,類其實也是對象,首先type生成了<class 'int'>這個對象,<class 'int'>又生成了1這個對象,type --> int --> 1

同樣,type生成了<class 'str'>這個對象,<class 'type'>又生成了"abc"這個對象,type --> str--> “abc”,即type -->生成類對象 -->對象

object   所有類的最頂層基類是object

print("int 的基類是:{}".format(int.__bases__))
print("str 的基類是:{}".format(str.__bases__))

result:

int 的基類是:(<class 'object'>,)
str 的基類是:(<class 'object'>,)
<class 'int'>和<class 'str'>的基類都是 <class 'object'> 即:object是最頂層的基類

type與object的關系(type的基類是object,object是type生成的,object的基類為空)

print("type 的基類是:{}".format(type.__bases__))
print("type object:{}".format(type(object)))
print("object 的基類是:{}".format(object.__bases__))

result:

type 的基類是:(<class 'object'>,)
type object:<class 'type'>
object 的基類是:()

詳解Python中的type和object 

總結

以上所述是小編給大家介紹的Python中type和object,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI