国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Python基于內置函數type創建新類型

瀏覽:87日期:2022-07-07 14:51:37

英文文檔:

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.

返回對象的類型,或者根據傳入的參數創建一個新的類型

說明:

1. 函數只傳入一個參數時,返回參數對象的類型。 返回值是一個類型對象,通常與對象.__ class__返回的對象相同。

#定義類型A>>> class A: name = ’defined in A’#創建類型A實例a>>> a = A()#a.__class__屬性>>> a.__class__<class ’__main__.A’>#type(a)返回a的類型>>> type(a)<class ’__main__.A’>#測試類型>>> type(a) == ATrue

 2. 雖然可以通過type函數來檢測一個對象是否是某個類型的實例,但是更推薦使用isinstance函數,因為isinstance函數考慮了父類子類間繼承關系。

#定義類型B,繼承A>>> class B(A): age = 2#創建類型B的實例b>>> b = B()#使用type函數測試b是否是類型A,返回False>>> type(b) == AFalse#使用isinstance函數測試b是否類型A,返回True>>> isinstance(b,A)True

 3. 函數另一種使用方式是傳入3個參數,函數將使用3個參數來創建一個新的類型。其中第一個參數name將用作新的類型的類名稱,即類型的__name__屬性;第二個參數是一個元組類型,其元素的類型均為類類型,將用作新創建類型的基類,即類型的__bases__屬性;第三個參數dict是一個字典,包含了新創建類的主體定義,即其值將復制到類型的__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函數創建類型D,含有屬性InfoD>>> D = type(’D’,(A,B),dict(InfoD=’some thing defined in D’))#C、D的類型>>> C<class ’__main__.C’>>>> D<class ’__main__.D’>#分別創建類型C、類型D的實例>>> c = C()>>> d = D()#分別輸出實例c、實例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’)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 久久久久久久国产a∨ | 视频一区二区在线 | 一级aaa级毛片午夜在线播放 | 亚洲精品国产综合99久久一区 | 美女扒开腿让男人桶个爽 | 成人欧美视频在线观看 | 国产精品日韩欧美一区二区 | 亚洲国产精品自产拍在线播放 | 国产免费高清福利拍拍拍 | 亚洲专区在线视频 | 亚洲欧洲国产精品 | 国产黄色三级三级三级 | 在线观看久草视频 | 成人黄色一级片 | 韩国三级日本三级香港三级黄 | 国产嫩草影院在线观看 | 久久精品国产99久久99久久久 | 欧美成人午夜做爰视频在线观看 | 精品国产精品 | 国产真实搭讪系列 | 99精品小视频 | 成人一区二区免费中文字幕 | 欧洲国产伦久久久久久久 | 怡红院美国十次成人影院 | 久久成人免费 | 成人a毛片久久免费播放 | 成人欧美一级毛片免费观看 | 中文久草 | 手机在线日韩高清理论片 | 91久久国产精品 | 目韩一区二区三区系列片丶 | 国产亚洲欧美日韩综合综合二区 | 欧美一级毛片大片免费播放 | 国产一区二区三区在线观看影院 | 亚洲综合成人网 | 欧美精品成人一区二区在线观看 | 亚洲国产第一区二区香蕉日日 | www.91亚洲| 免费看a级 | 久久93精品国产91久久综合 | 欧美日韩一区二区在线观看 |