深入理解 python 虛擬機(jī):花里胡哨的魔術(shù)方法
在本篇文章當(dāng)中主要給大家介紹在 cpython 當(dāng)中一些比較花里胡哨的魔術(shù)方法,以幫助我們自己實(shí)現(xiàn)比較花哨的功能,當(dāng)然這其中也包含一些也非常實(shí)用的魔術(shù)方法。
深入分析 hash 方法
在 Python 中,__hash__()
方法是一種特殊方法(也稱為魔術(shù)方法或雙下劃線方法),用于返回對(duì)象的哈希值。哈希值是一個(gè)整數(shù),用于在字典(dict
)和集合(set
)等數(shù)據(jù)結(jié)構(gòu)中進(jìn)行快速查找和比較。__hash__()
方法在創(chuàng)建自定義的可哈希對(duì)象時(shí)非常有用,例如自定義類的實(shí)例,以便可以將這些對(duì)象用作字典的鍵或集合的元素。
下面是一些需要注意的問題和示例來幫助理解 __hash__()
方法:
- 如果兩個(gè)對(duì)象相等(根據(jù)
__eq__()
方法的定義),它們的哈希值應(yīng)該相等。即,如果a == b
為真,則hash(a) == hash(b)
也為真,這一點(diǎn)非常重要,因?yàn)槲覀冊(cè)谑褂眉虾妥值涞臅r(shí)候,就需要保證容器當(dāng)中每種對(duì)象只能夠有一個(gè),如果不滿足這個(gè)條還的話,那么就可能會(huì)導(dǎo)致同一種對(duì)象在容器當(dāng)中會(huì)存在多個(gè)。 - 重寫
__hash__()
方法通常需要同時(shí)重寫__eq__()
方法,以確保對(duì)象的相等性和哈希值的一致性。 - 如果對(duì)象沒有定義
__eq__
方法,那么也不要定義__hash__
方法,因?yàn)槿绻龅焦V迪嗟鹊膶?duì)象時(shí)候,如果無法對(duì)兩個(gè)對(duì)象進(jìn)行比較的話,那么也會(huì)導(dǎo)致容易當(dāng)中有多個(gè)相同的對(duì)象。
import random
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __eq__(self, other):
return self.name == other.name and self.age == other.age
def __hash__(self):
return hash((self.name, self.age)) + random.randint(0, 1024)
def __repr__(self):
return f"[name={self.name}, age={self.age}]"
person1 = Person("Alice", 25)
person2 = Person("Alice", 25)
print(hash(person1))
print(hash(person2))
container = set()
container.add(person1)
container.add(person2)
print(container)
在上面代碼當(dāng)中我們重寫了 __hash__
函數(shù),但是對(duì)象的哈希值每次調(diào)用的時(shí)候我們都加入一個(gè)隨機(jī)數(shù),因此即使 name 和 age 都相等,如果 hash 值不想等,那么可能會(huì)造成容器當(dāng)中存在多個(gè)相同的對(duì)象,上面的代碼就會(huì)造成相同的對(duì)象,上面的程序輸出結(jié)果如下所示:
1930083569156318318
1930083569156318292
{[name=Alice, age=25], [name=Alice, age=25]}
如果重寫上面的類對(duì)象:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __eq__(self, other):
return self.name == other.name and self.age == other.age
def __hash__(self):
return hash((self.name, self.age))
def __repr__(self):
return f"[name={self.name}, age={self.age}]"
那么容器器當(dāng)中只會(huì)有一個(gè)對(duì)象。
如果我們只重寫了 __hash__
方法的時(shí)候也會(huì)造成容器當(dāng)中有多個(gè)相同的對(duì)象。
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# def __eq__(self, other):
# return self.name == other.name and self.age == other.age
def __hash__(self):
return hash((self.name, self.age)) # + random.randint(0, 1024)
def __repr__(self):
return f"[name={self.name}, age={self.age}]"
這是因?yàn)槿绻V迪嗤臅r(shí)候還需要在比較兩個(gè)對(duì)象是否相等,如果相等那么就不需要將這個(gè)對(duì)象保存到容器當(dāng)中,如果不相等那么將會(huì)將這個(gè)對(duì)象加入到容器當(dāng)中。
bool 方法
在 Python 中,object.__bool__()
方法是一種特殊方法,用于定義對(duì)象的布爾值。它在使用布爾運(yùn)算符(如 if
語句和邏輯運(yùn)算)時(shí)自動(dòng)調(diào)用。__bool__()
方法應(yīng)該返回一個(gè)布爾值,表示對(duì)象的真值。如果 __bool__()
方法未定義,Python 將嘗試調(diào)用 __len__()
方法來確定對(duì)象的真值。如果 __len__()
方法返回零,則對(duì)象被視為假;否則,對(duì)象被視為真。
下面是一些需要注意的事項(xiàng)來幫助理解 __bool__()
方法:
-
__bool__()
方法在對(duì)象被應(yīng)用布爾運(yùn)算時(shí)自動(dòng)調(diào)用。例如,在if
語句中,對(duì)象的真值由__bool__()
方法確定。 -
__bool__()
方法應(yīng)該返回一個(gè)布爾值(True
或False
)。 - 如果
__bool__()
方法未定義,Python 將嘗試調(diào)用__len__()
方法來確定對(duì)象的真值。 - 當(dāng)對(duì)象的長度為零時(shí),即
__len__()
方法返回零,對(duì)象被視為假;否則,對(duì)象被視為真。 - 如果既未定義
__bool__()
方法,也未定義__len__()
方法,則對(duì)象默認(rèn)為真。
下面是一個(gè)示例,展示了如何在自定義類中使用 __bool__()
方法:
class NonEmptyList:
def __init__(self, items):
self.items = items
def __bool__(self):
return len(self.items) > 0
my_list = NonEmptyList([1, 2, 3])
if my_list:
print("The list is not empty.")
else:
print("The list is empty.")
對(duì)象的屬性訪問
在Python中,我們可以通過一些特殊方法來定制屬性訪問的行為。本文將深入介紹這些特殊方法,包括__getitem__()
、__setitem__()
、__delitem__()
和__getattr__()
方法,以幫助更好地理解屬性訪問的機(jī)制和應(yīng)用場(chǎng)景。
__getitem__()
方法是用于索引操作的特殊方法。當(dāng)我們通過索引訪問對(duì)象的屬性時(shí),Python會(huì)自動(dòng)調(diào)用該方法,并傳入索引值作為參數(shù)。我們可以在該方法中實(shí)現(xiàn)對(duì)屬性的獲取操作,并返回相應(yīng)的值。
class MyList:
def __init__(self):
self.data = []
def __getitem__(self, index):
return self.data[index]
my_list = MyList()
my_list.data = [1, 2, 3]
print(my_list[1]) # 輸出: 2
在上面的例子中,我們定義了一個(gè)名為MyList的類,它具有一個(gè)屬性data,該屬性是一個(gè)列表。通過重寫__getitem__()方法,我們使得可以通過索引來訪問MyList對(duì)象的data屬性。當(dāng)我們使用my_list[1]的形式進(jìn)行索引操作時(shí),Python會(huì)自動(dòng)調(diào)用__getitem__()方法,并將索引值1作為參數(shù)傳遞給該方法。
__setitem__()
方法用于屬性的設(shè)置操作,即通過索引為對(duì)象的屬性賦值。當(dāng)我們使用索引操作并賦值給對(duì)象的屬性時(shí),Python會(huì)自動(dòng)調(diào)用__setitem__()
方法,并傳入索引值和賦值的值作為參數(shù)。
class MyList:
def __init__(self):
self.data = [0 for i in range(2)]
def __setitem__(self, index, value):
self.data[index] = value
my_list = MyList()
my_list[0] = 1
my_list[1] = 2
print(my_list.data) # 輸出: [1, 2]
在上述示例中,我們重寫了__setitem__()
方法來實(shí)現(xiàn)對(duì)對(duì)象屬性的設(shè)置操作。當(dāng)我們執(zhí)行my_list[0] = 1和my_list[1] = 2的賦值操作時(shí),Python會(huì)自動(dòng)調(diào)用__setitem__()
方法,并將索引值和賦值的值傳遞給該方法。在__setitem__()
方法中,我們將值賦給了對(duì)象的data屬性的相應(yīng)索引位置。
__delitem__()
方法用于刪除對(duì)象屬性的特殊方法。當(dāng)我們使用del語句刪除對(duì)象屬性時(shí),Python會(huì)自動(dòng)調(diào)用__delitem__()
方法,并傳入要?jiǎng)h除的屬性的索引值作為參數(shù)。
class MyDict:
def __init__(self):
self.data = dict()
def __delitem__(self, key):
print("In __delitem__")
del self.data[key]
obj = MyDict()
obj.data["key"] = "val"
del obj["key"] # 輸出 In __delitem__
__getattr__()
是一個(gè)特殊方法,用于在訪問不存在的屬性時(shí)自動(dòng)調(diào)用。它接收一個(gè)參數(shù),即屬性名,然后返回相應(yīng)的值或引發(fā) AttributeError
異常。
class MyClass:
def __getattr__(self, name):
if name == 'color':
return 'blue'
else:
raise AttributeError(f"'MyClass' object has no attribute '{name}'")
my_obj = MyClass()
print(my_obj.color) # 輸出: blue
print(my_obj.size) # 引發(fā) AttributeError: 'MyClass' object has no attribute 'size'
在上面的示例中,當(dāng)訪問 my_obj.color
時(shí),由于 color
屬性不存在,Python 會(huì)自動(dòng)調(diào)用 __getattr__()
方法,并返回預(yù)定義的值 'blue'
。而當(dāng)訪問 my_obj.size
時(shí),由于該屬性也不存在,__getattr__()
方法會(huì)引發(fā) AttributeError
異常。
__setattr__()
是一個(gè)特殊方法,用于在設(shè)置屬性值時(shí)自動(dòng)調(diào)用。它接收兩個(gè)參數(shù),即屬性名和屬性值。我們可以在該方法中對(duì)屬性進(jìn)行處理、驗(yàn)證或記錄。
class MyClass:
def __init__(self):
self.color = 'red' # 輸出:Setting attribute 'color' to 'red'
def __setattr__(self, name, value):
print(f"Setting attribute '{name}' to '{value}'")
super().__setattr__(name, value)
my_obj = MyClass()
my_obj.color = 'blue' # 輸出: Setting attribute 'color' to 'blue'
當(dāng)我們使用 . 的方式去訪問對(duì)象屬性的時(shí)候,首先會(huì)調(diào)用對(duì)象的 __getattribute__
函數(shù),如果屬性不存在才會(huì)調(diào)用 __getattr__
。當(dāng) __getattribute__
方法無法找到指定的屬性時(shí),Python 會(huì)調(diào)用 __getattr__
方法。以下是在之前的示例類 CustomClass
上添加 __getattr__
方法的代碼:
class CustomClass:
def __init__(self):
self.attribute = "Hello, world!"
def __getattribute__(self, name):
print(f"Accessing attribute: {name}")
return super().__getattribute__(name)
def __getattr__(self, name):
print(f"Attribute {name} not found")
return None
在這個(gè)示例中,我們?cè)?CustomClass
中添加了 __getattr__
方法。當(dāng) __getattribute__
方法無法找到指定的屬性時(shí),會(huì)自動(dòng)調(diào)用 __getattr__
方法,并打印出屬性名稱 "attribute" 以及未找到屬性的提示信息。
我們執(zhí)行下面的代碼:
obj = CustomClass()
print(obj.attribute)
print(obj.nonexistent_attribute)
輸出結(jié)果如下所示:
Accessing attribute: attribute
Hello, world!
Accessing attribute: nonexistent_attribute
Attribute nonexistent_attribute not found
None
首先,我們?cè)L問存在的屬性 attribute
,此時(shí) __getattribute__
方法被調(diào)用,并打印出屬性名稱 "attribute",然后返回屬性的實(shí)際值 "Hello, world!"。接著,我們嘗試訪問不存在的屬性 nonexistent_attribute
,由于 __getattribute__
方法無法找到該屬性,因此會(huì)調(diào)用 __getattr__
方法,并打印出屬性名稱 "nonexistent_attribute" 以及未找到屬性的提示信息,然后返回 None
。
上下文管理器
當(dāng)我們需要在特定的代碼塊執(zhí)行前后進(jìn)行一些操作時(shí),上下文管理器是一種非常有用的工具。上下文管理器可以確保資源的正確分配和釋放,無論代碼塊是否出現(xiàn)異常。在Python中,我們可以通過實(shí)現(xiàn) __enter__
和 __exit__
方法來創(chuàng)建自定義的上下文管理器。
下面是一個(gè)簡單的上下文管理器示例,展示了如何使用 object.__enter__
和 object.__exit__
方法來創(chuàng)建一個(gè)文件操作的上下文管理器:
class FileContextManager:
def __init__(self, filename, mode):
self.filename = filename
self.mode = mode
self.file = None
def __enter__(self):
self.file = open(self.filename, self.mode)
return self.file
def __exit__(self, exc_type, exc_value, traceback):
self.file.close()
with FileContextManager('example.txt', 'w') as file:
file.write('Hello, world!')
在上述示例中,FileContextManager
類實(shí)現(xiàn)了 __enter__
和 __exit__
方法。在 __enter__
方法中,我們打開文件并返回文件對(duì)象,這樣在 with
語句塊中就可以使用該文件對(duì)象。在 __exit__
方法中,我們關(guān)閉文件。
無論代碼塊是否拋出異常,__exit__
方法都會(huì)被調(diào)用來確保文件被正確關(guān)閉。這樣可以避免資源泄露和文件鎖定等問題。使用上下文管理器可以簡化代碼,并提供一致的資源管理方式,特別適用于需要打開和關(guān)閉資源的情況,如文件操作、數(shù)據(jù)庫連接等。
上述上下文管理器的 __exit__
方法有三個(gè)參數(shù):exc_type
、exc_value
和 traceback
。下面是對(duì)這些參數(shù)的詳細(xì)介紹:
-
exc_type
(異常類型):這個(gè)參數(shù)表示引發(fā)的異常的類型。如果在上下文管理器的代碼塊中沒有引發(fā)異常,它的值將為None
。如果有異常被引發(fā),exc_type
將是引發(fā)異常的類型。 -
exc_value
(異常值):這個(gè)參數(shù)表示引發(fā)的異常的實(shí)例。它包含了關(guān)于異常的詳細(xì)信息,如錯(cuò)誤消息。如果沒有異常被引發(fā),它的值也將為None
。 -
traceback
(回溯信息):這個(gè)參數(shù)是一個(gè)回溯對(duì)象,它包含了關(guān)于異常的堆棧跟蹤信息。它提供了導(dǎo)致異常的代碼路徑和調(diào)用關(guān)系。如果沒有異常被引發(fā),它的值將為None
。
總結(jié)
在本篇文章當(dāng)中主要給大家介紹了一些常用和比較重要的魔術(shù)方法,這些方法在我們平時(shí)用的可能也比較多,比如 hash 和 eq 還有對(duì)象的屬性訪問,為了方便異常處理可以使用 exit 和 enter 這個(gè)方法,其實(shí)還有很多其他的魔術(shù)方法,這些方法在 python 官網(wǎng)都有介紹,可以直接訪問 https://docs.python.org/3/reference/datamodel.html 。
本篇文章是深入理解 python 虛擬機(jī)系列文章之一,文章地址:https://github.com/Chang-LeHung/dive-into-cpython
更多精彩內(nèi)容合集可訪問項(xiàng)目:https://github.com/Chang-LeHung/CSCore文章來源:http://www.zghlxwxcb.cn/news/detail-456464.html
關(guān)注公眾號(hào):一無是處的研究僧,了解更多計(jì)算機(jī)(Java、Python、計(jì)算機(jī)系統(tǒng)基礎(chǔ)、算法與數(shù)據(jù)結(jié)構(gòu))知識(shí)。文章來源地址http://www.zghlxwxcb.cn/news/detail-456464.html
到了這里,關(guān)于深入理解 python 虛擬機(jī):花里胡哨的魔術(shù)方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!