新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythontype()
內(nèi)置函數(shù)type()用于返回指定對象的類型,它還允許根據(jù)給定的參數(shù)返回新類型的對象。

**type(object)** #where object is whoes type needs to be return
**type(name, bases, dict)**
類型()參數(shù):
取三個參數(shù)。type()函數(shù)有助于在驗證失敗時打印參數(shù)的類型。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 名字 | 類名;成為 name 屬性 | 需要 |
| 基礎(chǔ) | 列舉基類的元組;成為 bases 屬性 | 可選擇的 |
| 字典 | 字典,它是包含類主體定義的命名空間;成為 dict 屬性。 | 可選擇的 |
類型()返回值
如果傳遞了單個參數(shù),其值與對象相同。class 實例變量。如果傳遞了三個參數(shù),它會動態(tài)創(chuàng)建一個新類。
| 投入 | 返回值 | | 僅當(dāng)對象 | 對象類型 | | If 3 參數(shù) | 新的類型對象 |
Python 中type()方法的示例
示例 1:如何獲取對象的類型
number_list = [3, 4]
print(type(number_list))
number_dict = {3: 'three', 4: 'four'}
print(type(number_dict))
class Foo:
a = 0
foo = Foo()
print(type(foo))
輸出:
< class 'list'>
< class 'dict'>
< class '__main__.Foo'>
示例 2:如何創(chuàng)建類型對象
obj1 = type('X', (object,), dict(a='Foo', b=12))
print(type(obj1))
print(vars(obj1))
class test:
a = 'Fo'
b = 15
obj2 = type('Y', (test,), dict(a='Foo', b=12))
print(type(obj2))
print(vars(obj2))
輸出:
{'a': 'Fo', 'b': 15, '__module__': '__main__', '__dict__': , '__weakref__': , '__doc__': None}
{'a': 'Fo', 'b': 15, '__module__': '__main__', '__doc__': None} 網(wǎng)頁題目:創(chuàng)新互聯(lián)Python教程:Pythontype()
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/dpijidh.html


咨詢
建站咨詢
