新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python中type是函數(shù)還是類?
python中的type() 函數(shù)如果只有第一個(gè)參數(shù)則返回對(duì)象的類型,三個(gè)參數(shù)返回新的類型對(duì)象。

isinstance() 與 type() 區(qū)別:
type() 不會(huì)認(rèn)為子類是一種父類類型,不考慮繼承關(guān)系。
isinstance() 會(huì)認(rèn)為子類是一種父類類型,考慮繼承關(guān)系。
如果要判斷兩個(gè)類型是否相同推薦使用 isinstance()。
語(yǔ)法
以下是 type() 方法的語(yǔ)法:
type(object) type(name, bases, dict)
參數(shù)
name -- 類的名稱。 bases -- 基類的元組。 dict -- 字典,類內(nèi)定義的命名空間變量。
返回值
一個(gè)參數(shù)返回對(duì)象類型, 三個(gè)參數(shù),返回新的類型對(duì)象。
以下展示了使用 type 函數(shù)的實(shí)例:
# 一個(gè)參數(shù)實(shí)例 >>> type(1)>>> type('runoob') >>> type([2]) >>> type({0:'zero'}) >>> x = 1 >>> type( x ) == int # 判斷類型是否相等 True # 三個(gè)參數(shù) >>> class X(object): ... a = 1 ... >>> X = type('X', (object,), dict(a=1)) # 產(chǎn)生一個(gè)新的類型 X >>> X
網(wǎng)頁(yè)名稱:創(chuàng)新互聯(lián)Python教程:python中type是函數(shù)還是類?
文章URL:http://m.fisionsoft.com.cn/article/dhigipi.html


咨詢
建站咨詢
