Python - 反射



在面向物件程式設計中,**反射**是指提取任何正在使用的物件資訊的能力。您可以瞭解物件的型別,它是否是任何其他類的子類,它的屬性是什麼,以及更多資訊。Python 的標準庫包含多個用於反射物件的各種屬性的函式。**反射**有時也稱為**內省**。

以下是 Python 中的反射函式列表:

  • type() 函式
  • isinstance() 函式
  • issubclass() 函式
  • callable() 函式
  • getattr() 函式
  • setattr() 函式
  • hasattr() 函式
  • dir() 函式

type() 函式

我們多次使用過此函式。它告訴您物件屬於哪個類。

示例

以下語句列印不同內建資料型別物件的相應類

print (type(10))
print (type(2.56))
print (type(2+3j))
print (type("Hello World"))
print (type([1,2,3]))
print (type({1:'one', 2:'two'}))

在這裡,您將獲得以下輸出

<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'list'>
<class 'dict'>

讓我們驗證使用者定義類物件的型別:

class test:
   pass
   
obj = test()
print (type(obj))

它將產生以下輸出

<class '__main__.test'>

isinstance() 函式

這是 Python 中的另一個內建函式,它確定物件是否是給定類的例項。

語法

isinstance(obj, class)

此函式始終返回布林值,如果物件確實屬於給定類,則返回 true,否則返回 false。

示例

以下語句返回 True:

print (isinstance(10, int))
print (isinstance(2.56, float))
print (isinstance(2+3j, complex))
print (isinstance("Hello World", str))

它將產生以下輸出

True
True
True
True

相反,這些語句列印 False。

print (isinstance([1,2,3], tuple))
print (isinstance({1:'one', 2:'two'}, set))

它將產生以下輸出

False
False

您還可以使用使用者定義的類進行檢查

class test:
   pass
   
obj = test()
print (isinstance(obj, test))

它將產生以下輸出

True

在 Python 中,即使類也是物件。所有類都是 object 類的物件。可以透過以下程式碼驗證:

class test:
   pass
   
print (isinstance(int, object))
print (isinstance(str, object))
print (isinstance(test, object))

以上所有 print 語句都列印 True。

issubclass() 函式

此函式檢查一個類是否是另一個類的子類。與類有關,而不是它們的例項。

如前所述,所有 Python 類都是從 object 類繼承的。因此,以下 print 語句的輸出對所有語句都為 True。

class test:
   pass
   
print (issubclass(int, object))
print (issubclass(str, object))
print (issubclass(test, object))

它將產生以下輸出

True
True
True

callable() 函式

如果物件呼叫某個過程,則該物件是可呼叫的。Python 函式執行某個過程,是一個可呼叫物件。因此 callable(function) 返回 True。任何函式(內建的、使用者定義的或方法)都是可呼叫的。內建資料型別(如 int、str 等)的物件不可呼叫。

示例

def test():
   pass
   
print (callable("Hello"))
print (callable(abs))
print (callable(list.clear([1,2])))
print (callable(test))

字串物件不可呼叫。但 abs 是一個可呼叫的函式。list 的 pop 方法是可呼叫的,但clear()實際上是對函式的呼叫,而不是函式物件,因此不可呼叫。

它將產生以下輸出

False
True
True
False
True

如果一個類的例項具有__call__()方法,則它是可呼叫的。在下面的示例中,test 類包含__call__()方法。因此,它的物件可以像呼叫函式一樣使用。因此,具有__call__()方法的類的物件是可呼叫的。

class test:
   def __init__(self):
      pass
   def __call__(self):
      print ("Hello")
      
obj = test()
obj()
print ("obj is callable?", callable(obj))

它將產生以下輸出

Hello
obj is callable? True

getattr() 函式

內建函式getattr()檢索物件的命名屬性的值。

示例

class test:
   def __init__(self):
      self.name = "Manav"
      
obj = test()
print (getattr(obj, "name"))

它將產生以下輸出

Manav

setattr() 函式

內建函式setattr()向物件新增一個新屬性併為其賦值。它也可以更改現有屬性的值。

在下面的示例中,test 類的物件只有一個屬性 - name。我們使用setattr()新增 age 屬性並修改 name 屬性的值。

class test:
   def __init__(self):
      self.name = "Manav"
      
obj = test()
setattr(obj, "age", 20)
setattr(obj, "name", "Madhav")
print (obj.name, obj.age)

它將產生以下輸出

Madhav 20

hasattr() 函式

此內建函式如果給定的屬性可用於物件引數,則返回 True,否則返回 false。我們使用相同的 test 類並檢查它是否具有某個屬性。

class test:
   def __init__(self):
      self.name = "Manav"
      
obj = test()
print (hasattr(obj, "age"))
print (hasattr(obj, "name"))

它將產生以下輸出

False
True

dir() 函式

如果此內建函式不帶引數呼叫,則返回當前作用域中的名稱。對於任何作為引數的物件,它都會返回給定物件及其可訪問屬性的列表。

  • 對於模組物件 - 該函式返回模組的屬性。

  • 對於類物件 - 該函式返回其屬性,並遞迴返回其基類的屬性。

  • 對於任何其他物件 - 其屬性、其類的屬性及其類基類的屬性(遞迴)。

示例

print ("dir(int):", dir(int))

它將產生以下輸出

dir(int): ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_count', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

示例

print ("dir(dict):", dir(dict))

它將產生以下輸出

dir(dict): ['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

示例

class test:
   def __init__(self):
      self.name = "Manav"

obj = test()
print ("dir(obj):", dir(obj))

它將產生以下輸出

dir(obj): ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
廣告