python函数type的用意_python中type()是什么意思
type()是一個內建的獲取變量類型的函數。
type()函數有兩個用法,當只有一個參數的時候,返回對象的類型。當有三個參數的時候返回一個類對象。
語法:type(object)
type(name, bases, dict)
具體用法:
一個參數type(object)
返回一個對象的類型,如:In [1]: a = 10
In [2]: type(a)
Out[2]: int
三個參數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")
# 靜態方法
@staticmethod
def statictest():
print("this is a static method")
# 創建類
test_property = {"name": "tom", "instancetest": instancetest, "classtest": classtest, "statictest": statictest}
Test = type("Test", (), test_property)
# 創建對象
test = Test()
# 調用方法
print(test.name)
test.instancetest()
test.classtest()
test.statictest()
輸出結果:tom
this is a instance method
this is a class method
this is a static method
以上就是python中type()是什么意思的詳細內容,更多請關注隨便開發網其它相關文章!
總結
以上是生活随笔為你收集整理的python函数type的用意_python中type()是什么意思的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: security center拒绝访问_
- 下一篇: python可以代替plc吗_PLC可以