詳解Python中的type和object
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的關(guān)系(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 的基類是:()
總結(jié)
以上所述是小編給大家介紹的Python中type和object,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Python開發(fā)時報TypeError:?‘int‘?object?is?not?iterable錯誤的解決方式
- Python源碼學習之PyType_Type和PyBaseObject_Type詳解
- Python源碼學習之PyObject和PyTypeObject
- python報錯TypeError: ‘NoneType‘ object is not subscriptable的解決方法
- 解決Python 異常TypeError: cannot concatenate ''str'' and ''int'' objects
- Python:type、object、class與內(nèi)置類型實例
- Python 出現(xiàn)錯誤TypeError: ‘NoneType’ object is not iterable解決辦法
- 最新整理Python中的type和object的示例詳解
相關(guān)文章
python socket發(fā)送TCP數(shù)據(jù)方式
這篇文章主要介紹了python socket發(fā)送TCP數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09pytorch 求網(wǎng)絡(luò)模型參數(shù)實例
今天小編就為大家分享一篇pytorch 求網(wǎng)絡(luò)模型參數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12python數(shù)據(jù)結(jié)構(gòu)樹和二叉樹簡介
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)樹和二叉樹簡介,需要的朋友可以參考下2014-04-04關(guān)于python3的ThreadPoolExecutor線程池大小設(shè)置
這篇文章主要介紹了關(guān)于python3的ThreadPoolExecutor線程池大小設(shè)置,線程池的理想大小取決于被提交任務(wù)的類型以及所部署系統(tǒng)的特性,需要的朋友可以參考下2023-04-04