詳解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中是一切皆對象的,類其實(shí)也是對象,首先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ā)時報(bào)TypeError:?‘int‘?object?is?not?iterable錯誤的解決方式
- Python源碼學(xué)習(xí)之PyType_Type和PyBaseObject_Type詳解
- Python源碼學(xué)習(xí)之PyObject和PyTypeObject
- python報(bào)錯TypeError: ‘NoneType‘ object is not subscriptable的解決方法
- 解決Python 異常TypeError: cannot concatenate ''str'' and ''int'' objects
- Python:type、object、class與內(nèi)置類型實(shí)例
- 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ù)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09python模擬嗶哩嗶哩滑塊登入驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了python模擬嗶哩嗶哩滑塊登入驗(yàn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04pytorch 求網(wǎng)絡(luò)模型參數(shù)實(shí)例
今天小編就為大家分享一篇pytorch 求網(wǎng)絡(luò)模型參數(shù)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12使用python的Flask框架進(jìn)行上傳和下載文件詳解
這篇文章主要介紹了使用python的Flask框架進(jìn)行上傳和下載文件詳解,Flask是一個使用Pyhton編寫的輕量級Web應(yīng)用框架,工具包采用Werkzeug,模板引擎則使用Jinja2,是目前十分流行的web框架,需要的朋友可以參考下2023-07-07python數(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