Python中type的用法


描述

python的 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

使用help打印Test的詳細信息:

class Test(builtins.object)                                               
 |  Methods defined here:                                                 
 |                                                                        
 |  instancetest(self)                                                    
 |      # 實例方法                                                            
 |                                                                        
 |  ----------------------------------------------------------------------
 |  Class methods defined here:                                           
 |                                                                        
 |  classtest() from builtins.type                                        
 |      # 類方法                                                             
 |                                                                        
 |  ----------------------------------------------------------------------
 |  Static methods defined here:                                          
 |                                                                        
 |  statictest()                                                          
 |      # 靜態方法                                                            
 |                                                                        
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:                                        
 |                                                                        
 |  __dict__                                                              
 |      dictionary for instance variables (if defined)                    
 |                                                                        
 |  __weakref__                                                           
 |      list of weak references to the object (if defined)                
 |                                                                        
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:                               
 |                                                                        
 |  name = 'tom'                                        

可以看出我們創建了一個Test類,包含一個實例方法statictest,類方法classtest,靜態方法statictest,和一個屬性name = 'tom'。

type和isinstance

type不會認為子類是父類的類型,不會考慮繼承關系。isinstance會任務子類是父類的類型,考慮繼承關系。

Type和Object

type為對象的頂點,所有對象都創建自type。

object為類繼承的頂點,所有類都繼承自object。

python中萬物皆對象,一個python對象可能擁有兩個屬性,__class____base____class__ 表示這個對象是誰創建的,__base__ 表示一個類的父類是誰。

In [1]: object.__class__
Out[1]: type

In [2]: type.__base__
Out[2]: object

可以得出結論:

  • type類繼承自object
  • object的對象創建自type


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM