詳解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,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(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)文章
python3.6.5基于kerberos認證的hive和hdfs連接調(diào)用方式
這篇文章主要介紹了python3.6.5基于kerberos認證的hive和hdfs連接調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
在arcgis使用python腳本進行字段計算時是如何解決中文問題的
這篇文章主要介紹了在arcgis使用python腳本進行字段計算時是如何解決中文問題的,需要的朋友可以參考下2015-10-10
詳解python如何正確使用時間戳,日期,時間,時區(qū)
這篇文章主要為大家介紹了如何在python中正確使用時間戳,日期,時間,時區(qū),文中通過簡單的示例進行了詳細介紹,希望對大家有一定的幫助2024-11-11

