亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python基于內(nèi)置函數(shù)type創(chuàng)建新類型

瀏覽:101日期: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.

返回對(duì)象的類型,或者根據(jù)傳入的參數(shù)創(chuàng)建一個(gè)新的類型

說(shuō)明:

1. 函數(shù)只傳入一個(gè)參數(shù)時(shí),返回參數(shù)對(duì)象的類型。 返回值是一個(gè)類型對(duì)象,通常與對(duì)象.__ class__返回的對(duì)象相同。

#定義類型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’>#測(cè)試類型>>> type(a) == ATrue

 2. 雖然可以通過(guò)type函數(shù)來(lái)檢測(cè)一個(gè)對(duì)象是否是某個(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ù)測(cè)試b是否是類型A,返回False>>> type(b) == AFalse#使用isinstance函數(shù)測(cè)試b是否類型A,返回True>>> isinstance(b,A)True

 3. 函數(shù)另一種使用方式是傳入3個(gè)參數(shù),函數(shù)將使用3個(gè)參數(shù)來(lái)創(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’)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 免费看美女无遮掩的软件 | 欧美日韩一区二区综合在线视频 | 国产主播福利精品一区二区 | 能在线观看的一区二区三区 | 国产精品永久免费自在线观看 | 国产精品自拍在线观看 | 亚洲爽爽| 久久国产亚洲欧美日韩精品 | 欧美日韩视频二区三区 | 男女午夜爱爱久久无遮挡 | 国产日本在线视频 | 国产精品久久久久久久久久直 | 欧美日韩日本国产 | 国产成人精品实拍在线 | 国产在线99| 一区二区三区在线 | 久久九九热视频 | 美女亚洲视频 | 自怕偷自怕亚洲精品 | 7799国产精品久久久久99 | 久久精品国产第一区二区 | 欧美一级专区免费大片俄罗斯 | 91影视永久福利免费观看 | 亚洲成人国产精品 | 亚洲情a成黄在线观看动 | 99精品视频一区在线视频免费观看 | 亚州国产 | 国产免费一级高清淫曰本片 | 一区二区三区 亚洲区 | 国产一区二区三区高清 | 天堂在线www网亚洲 天堂在线视频网站 | 国产精品久久久久久久免费 | 亚洲精品在线影院 | 成人毛片1024你懂的 | 日韩精品小视频 | 中文字幕在线网址 | 亚洲欧美精品一中文字幕 | 成人亚洲精品 | 91精品久久久久亚洲国产 | 欧美精品日本一级特黄 | 韩国一大片a毛片 |