溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python中的type函數(shù)的用法

發(fā)布時間:2020-05-18 17:02:24 來源:億速云 閱讀:899 作者:Leah 欄目:編程語言

這篇文章主要為大家詳細(xì)介紹了python中的type函數(shù)的用法,文中示例代碼簡明扼要容易理解,零基礎(chǔ)也能參考此文章,感興趣的小伙伴們可以參考一下。

type()是一個內(nèi)建的獲取變量類型的函數(shù)。

type()函數(shù)有兩個用法,當(dāng)只有一個參數(shù)的時候,返回對象的類型。當(dāng)有三個參數(shù)的時候返回一個類對象。

語法:

type(object)
type(name, bases, dict)

具體用法:

一個參數(shù)

type(object)

返回一個對象的類型,如:

In [1]: a = 10 
In [2]: type(a)
Out[2]: int

三個參數(shù)

tpye(name, bases, dict)

name 類名

bases 父類的元組

dict 類的屬性方法和值組成的鍵值對

返回一個類對象:

# 實例方法
def instancetest(self):
	print("this is a instance method")
# 類方法
@classmethod
def classtest(cls):
	print("this is a class method")
# 靜態(tài)方法
@staticmethod
def statictest():
	print("this is a static method")
# 創(chuàng)建類
test_property = {"name": "tom", "instancetest": instancetest, "classtest": classtest, "statictest": statictest}
Test = type("Test", (), test_property)
# 創(chuàng)建對象
test = Test()
# 調(diào)用方法
print(test.name)
test.instancetest()
test.classtest()
test.statictest()

輸出結(jié)果:

tom
this is a instance method
this is a class method
this is a static method

關(guān)于python中的type函數(shù)的用法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果喜歡這篇文章,不如把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI