Python基于內(nèi)置函數(shù)type創(chuàng)建新類型
英文文檔:
class type(object)
class type(name, bases, dict)
With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.__class__.
The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.
With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The namestring is the class name and becomes the __name__ attribute; the bases tuple itemizes the base classes and becomes the __bases__ attribute; and the dict dictionary is the namespace containing definitions for class body and is copied to a standard dictionary to become the __dict__ attribute.
返回對象的類型,或者根據(jù)傳入的參數(shù)創(chuàng)建一個(gè)新的類型
說明:
1. 函數(shù)只傳入一個(gè)參數(shù)時(shí),返回參數(shù)對象的類型。 返回值是一個(gè)類型對象,通常與對象.__ class__返回的對象相同。
#定義類型A >>> class A: name = 'defined in A' #創(chuàng)建類型A實(shí)例a >>> a = A() #a.__class__屬性 >>> a.__class__ <class '__main__.A'> #type(a)返回a的類型 >>> type(a) <class '__main__.A'> #測試類型 >>> type(a) == A True
2. 雖然可以通過type函數(shù)來檢測一個(gè)對象是否是某個(gè)類型的實(shí)例,但是更推薦使用isinstance函數(shù),因?yàn)閕sinstance函數(shù)考慮了父類子類間繼承關(guān)系。
#定義類型B,繼承A >>> class B(A): age = 2 #創(chuàng)建類型B的實(shí)例b >>> b = B() #使用type函數(shù)測試b是否是類型A,返回False >>> type(b) == A False #使用isinstance函數(shù)測試b是否類型A,返回True >>> isinstance(b,A) True
3. 函數(shù)另一種使用方式是傳入3個(gè)參數(shù),函數(shù)將使用3個(gè)參數(shù)來創(chuàng)建一個(gè)新的類型。其中第一個(gè)參數(shù)name將用作新的類型的類名稱,即類型的__name__屬性;第二個(gè)參數(shù)是一個(gè)元組類型,其元素的類型均為類類型,將用作新創(chuàng)建類型的基類,即類型的__bases__屬性;第三個(gè)參數(shù)dict是一個(gè)字典,包含了新創(chuàng)建類的主體定義,即其值將復(fù)制到類型的__dict__屬性中。
#定義類型A,含有屬性InfoA >>> class A(object): InfoA = 'some thing defined in A' #定義類型B,含有屬性InfoB >>> class B(object): InfoB = 'some thing defined in B' #定義類型C,含有屬性InfoC >>> class C(A,B): InfoC = 'some thing defined in C' #使用type函數(shù)創(chuàng)建類型D,含有屬性InfoD >>> D = type('D',(A,B),dict(InfoD='some thing defined in D')) #C、D的類型 >>> C <class '__main__.C'> >>> D <class '__main__.D'> #分別創(chuàng)建類型C、類型D的實(shí)例 >>> c = C() >>> d = D() #分別輸出實(shí)例c、實(shí)例b的屬性 >>> (c.InfoA,c.InfoB,c.InfoC) ('some thing defined in A', 'some thing defined in B', 'some thing defined in C') >>> (d.InfoA,d.InfoB,d.InfoD) ('some thing defined in A', 'some thing defined in B', 'some thing defined in D')
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python可設(shè)置抽獎(jiǎng)?wù)邫?quán)重的抽獎(jiǎng)腳本代碼
這篇文章主要介紹了Python可設(shè)置抽獎(jiǎng)?wù)邫?quán)重的抽獎(jiǎng)腳本,抽獎(jiǎng)系統(tǒng)包含可給不同抽獎(jiǎng)?wù)咴O(shè)置不同的權(quán)重,先從價(jià)值高的獎(jiǎng)品開始抽,已經(jīng)中獎(jiǎng)的人,不再參與后續(xù)的抽獎(jiǎng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11Python設(shè)計(jì)模式之抽象工廠模式原理與用法詳解
這篇文章主要介紹了Python設(shè)計(jì)模式之抽象工廠模式,簡單講述了抽象工廠模式的概念、原理并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)與使用抽象工廠模式的相關(guān)操作技巧,需要的朋友可以參考下2019-01-01python+selenium實(shí)現(xiàn)QQ郵箱自動(dòng)發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了python+selenium實(shí)現(xiàn)QQ郵箱自動(dòng)發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01python中json.dumps()和json.loads()的用法
json.dumps()和json.loads()?json.dumps()用于將字典形式轉(zhuǎn)換為字符串,下面這篇文章主要給大家介紹了關(guān)于python中json.dumps()和json.loads()用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09解決python 執(zhí)行shell命令無法獲取返回值的問題
這篇文章主要介紹了解決python 執(zhí)行shell命令無法獲取返回值的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12