當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python type()用法及代碼示例

type()方法返回作為參數傳遞的參數(對象)的類類型。 type()函數主要用於調試目的。

可以將兩種不同類型的參數傳遞給type()函數,即單參數和三參數。如果是單個參數type(obj)傳遞後,它返回給定對象的類型。如果三個參數type(name, bases, dict)傳遞後,它返回一個新的類型對象。

用法:


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

參數:

name:類的名稱,該名稱後來對應於該類的__name__屬性。
bases:當前類派生的類的元組。以後對應於__bases__屬性。
dict:包含類的名稱空間的字典。以後對應於__dict__屬性。

返回類型:

returns a new type class or essentially a metaclass.

代碼1:

# Python3 simple code to explain 
# the type() function 
print(type([]) is list) 
  
print(type([]) is not list) 
  
print(type(()) is tuple) 
  
print(type({}) is dict) 
  
print(type({}) is not list)

輸出:

True
False
True
True
True

代碼2:

# Python3 code to explain 
# the type() function 
  
# Class of type dict 
class DictType:
    DictNumber = {1:'John', 2:'Wick', 
                  3:'Barry', 4:'Allen'} 
      
    # Will print the object type 
    # of existing class 
    print(type(DictNumber)) 
  
# Class of type list     
class ListType:
    ListNumber = [1, 2, 3, 4, 5] 
      
    # Will print the object type 
    # of existing class 
    print(type(ListNumber)) 
  
# Class of type tuple     
class TupleType:
    TupleNumber = ('Geeks', 'for', 'geeks') 
      
    # Will print the object type 
    # of existing class 
    print(type(TupleNumber)) 
  
# Creating object of each class     
d = DictType() 
l = ListType() 
t = TupleType()

輸出:

<class 'dict'>
<class 'list'>
<class 'tuple'>


代碼3:

# Python3 code to explain 
# the type() function 
  
# Class of type dict 
class DictType:
    DictNumber = {1:'John', 2:'Wick', 3:'Barry', 4:'Allen'} 
      
# Class of type list     
class ListType:
    ListNumber = [1, 2, 3, 4, 5] 
  
# Creating object of each class    
d = DictType() 
l = ListType() 
  
# Will print accordingly whether both 
# the objects are of same type or not   
if type(d) is not type(l):
    print("Both class have different object type.") 
else:
    print("Same Object type")

輸出:

Both class have different object type.


代碼4:用於type(name, bases, dict)

# Python3 program to demonstrate 
# type(name, bases, dict) 
  
# New class(has no base) class with the 
# dynamic class initialization of type() 
new = type('New', (object, ), 
      dict(var1 ='GeeksforGeeks', b = 2009)) 
  
# Print type() which returns class 'type' 
print(type(new)) 
print(vars(new)) 
  
  
# Base class, incorporated 
# in our new class 
class test:
    a = "Geeksforgeeks"
    b = 2009
  
# Dynamically initialize Newer class 
# It will derive from the base class test 
newer = type('Newer', (test, ), 
        dict(a ='Geeks', b = 2018)) 
          
print(type(newer)) 
print(vars(newer))

輸出:

{'__module__':'__main__', 'var1':'GeeksforGeeks', '__weakref__':, 'b':2009, '__dict__':, '__doc__':None}

{'b':2018, '__doc__':None, '__module__':'__main__', 'a':'Geeks'}


應用範圍:

  • type()函數本質上用於調試目的。如果將其他字符串函數(如.upper(),.lower(),.split())與從網絡抓取工具中提取的文本一起使用,則可能無法使用,因為它們的類型可能不同,不支持字符串函數。結果,它將繼續拋出錯誤,這些錯誤很難調試[將錯誤視為:GeneratorType沒有屬性lower()]。那時可以使用type()函數確定所提取文本的類型,然後在使用字符串函數或對其進行任何其他操作之前,將其更改為其他形式的字符串。
  • type()具有三個參數的參數可用於動態初始化類或具有屬性的現有類。它還用於通過SQL注冊數據庫表。



注:本文由純淨天空篩選整理自retr0大神的英文原創作品 Python | type() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。