詳解Python中魔法方法的使用
python中的魔法方法是一些可以讓你對(duì)類添加“魔法”的特殊方法,它們經(jīng)常是兩個(gè)下劃線包圍來命名的
Python的魔法方法,也稱為dunder(雙下劃線)方法。大多數(shù)的時(shí)候,我們將它們用于簡(jiǎn)單的事情,例如構(gòu)造函數(shù)(__init__)、字符串表示(__str__, __repr__)或算術(shù)運(yùn)算符(__add__/__mul__)。其實(shí)還有許多你可能沒有聽說過的但是卻很好用的方法,在這篇文章中,我們將整理這些魔法方法!
迭代器的大小
我們都知道__len__方法,可以用它在容器類上實(shí)現(xiàn)len()函數(shù)。但是,如果您想獲取實(shí)現(xiàn)迭代器的類對(duì)象的長(zhǎng)度怎么辦?
it = iter(range(100)) print(it.__length_hint__()) # 100 next(it) print(it.__length_hint__()) # 99 a = [1, 2, 3, 4, 5] it = iter(a) print(it.__length_hint__()) # 5 next(it) print(it.__length_hint__()) # 4 a.append(6) print(it.__length_hint__()) # 5
你所需要做的就是實(shí)現(xiàn)__length_hint__方法,這個(gè)方法是迭代器上的內(nèi)置方法(不是生成器),正如你上面看到的那樣,并且還支持動(dòng)態(tài)長(zhǎng)度更改。但是,正如他的名字那樣,這只是一個(gè)提示(hint),并不能保證完全準(zhǔn)確:對(duì)于列表迭代器,可以得到準(zhǔn)確的結(jié)果,但是對(duì)于其他迭代器則不確定。但是即使它不準(zhǔn)確,它也可以幫我們獲得需要的信息,正如PEP 424中解釋的那樣
length_hint must return an integer (else a TypeError is raised) or NotImplemented, and is not required to be accurate. It may return a value that is either larger or smaller than the actual size of the container. A return value of NotImplemented indicates that there is no finite length estimate. It may not return a negative value (else a ValueError is raised).
元編程
大部分很少看到的神奇方法都與元編程有關(guān),雖然元編程可能不是我們每天都需要使用的東西,但有一些方便的技巧可以使用它。
一個(gè)這樣的技巧是使用__init_subclass__作為擴(kuò)展基類功能的快捷方式,而不必處理元類:
class Pet: def __init_subclass__(cls, /, default_breed, **kwargs): super().__init_subclass__(**kwargs) cls.default_breed = default_breed class Dog(Pet, default_name="German Shepherd"): pass
上面的代碼我們向基類添加關(guān)鍵字參數(shù),該參數(shù)可以在定義子類時(shí)設(shè)置。在實(shí)際用例中可能會(huì)在想要處理提供的參數(shù)而不僅僅是賦值給屬性的情況下使用此方法。
看起來非?;逎⑶液苌贂?huì)用到,但其實(shí)你可能已經(jīng)遇到過很多次了,因?yàn)樗话愣际窃跇?gòu)建API時(shí)使用的,例如在SQLAlchemy或Flask Views中都使用到了。
另一個(gè)元類的神奇方法是__call__。這個(gè)方法允許自定義調(diào)用類實(shí)例時(shí)發(fā)生的事情:
class CallableClass: def __call__(self, *args, **kwargs): print("I was called!") instance = CallableClass() instance() # I was called!
可以用它來創(chuàng)建一個(gè)不能被調(diào)用的類:
class NoInstances(type): def __call__(cls, *args, **kwargs): raise TypeError("Can't create instance of this class") class SomeClass(metaclass=NoInstances): @staticmethod def func(x): print('A static method') instance = SomeClass() # TypeError: Can't create instance of this class
對(duì)于只有靜態(tài)方法的類,不需要?jiǎng)?chuàng)建類的實(shí)例就用到了這個(gè)方法。
另一個(gè)類似的場(chǎng)景是單例模式——一個(gè)類最多只能有一個(gè)實(shí)例:
class Singleton(type): def __init__(cls, *args, **kwargs): cls.__instance = None super().__init__(*args, **kwargs) def __call__(cls, *args, **kwargs): if cls.__instance is None: cls.__instance = super().__call__(*args, **kwargs) return cls.__instance else: return cls.__instance class Logger(metaclass=Singleton): def __init__(self): print("Creating global Logger instance")
Singleton類擁有一個(gè)私有__instance——如果沒有,它會(huì)被創(chuàng)建并賦值,如果它已經(jīng)存在,它只會(huì)被返回。
假設(shè)有一個(gè)類,你想創(chuàng)建它的一個(gè)實(shí)例而不調(diào)用__init__。__new__ 方法可以幫助解決這個(gè)問題:
class Document: def __init__(self, text): self.text = text bare_document = Document.__new__(Document) print(bare_document.text) # AttributeError: 'Document' object has no attribute 'text' setattr(bare_document, "text", "Text of the document")
在某些情況下,我們可能需要繞過創(chuàng)建實(shí)例的通常過程,上面的代碼演示了如何做到這一點(diǎn)。我們不調(diào)用Document(…),而是調(diào)用Document.__new__(Document),它創(chuàng)建一個(gè)裸實(shí)例,而不調(diào)用__init__。因此,實(shí)例的屬性(在本例中為text)沒有初始化,所欲我們需要額外使用setattr函數(shù)賦值(它也是一個(gè)魔法的方法__setattr__)。
為什么要這么做呢。因?yàn)槲覀兛赡軙?huì)想要替代構(gòu)造函數(shù),比如:
class Document: def __init__(self, text): self.text = text @classmethod def from_file(cls, file): # Alternative constructor d = cls.__new__(cls) # Do stuff... return d
這里定義from_file方法,它作為構(gòu)造函數(shù),首先使用__new__創(chuàng)建實(shí)例,然后在不調(diào)用__init__的情況下配置它。
下一個(gè)與元編程相關(guān)的神奇方法是__getattr__。當(dāng)普通屬性訪問失敗時(shí)調(diào)用此方法。這可以用來將對(duì)缺失方法的訪問/調(diào)用委托給另一個(gè)類:
class String: def __init__(self, value): self._value = str(value) def custom_operation(self): pass def __getattr__(self, name): return getattr(self._value, name) s = String("some text") s.custom_operation() # Calls String.custom_operation() print(s.split()) # Calls String.__getattr__("split") and delegates to str.split # ['some', 'text'] print("some text" + "more text") # ... works print(s + "more text") # TypeError: unsupported operand type(s) for +: 'String' and 'str'
我們想為類添加一些額外的函數(shù)(如上面的custom_operation)定義string的自定義實(shí)現(xiàn)。但是我們并不想重新實(shí)現(xiàn)每一個(gè)字符串方法,比如split、join、capitalize等等。這里我們就可以使用__getattr__來調(diào)用這些現(xiàn)有的字符串方法。
雖然這適用于普通方法,但請(qǐng)注意,在上面的示例中,魔法方法__add__(提供的連接等操作)沒有得到委托。所以,如果我們想讓它們也能正常工作,就必須重新實(shí)現(xiàn)它們。
自省(introspection)
最后一個(gè)與元編程相關(guān)的方法是__getattribute__。它一個(gè)看起來非常類似于前面的__getattr__,但是他們有一個(gè)細(xì)微的區(qū)別,__getattr__只在屬性查找失敗時(shí)被調(diào)用,而__getattribute__是在嘗試屬性查找之前被調(diào)用。
所以可以使用__getattribute__來控制對(duì)屬性的訪問,或者你可以創(chuàng)建一個(gè)裝飾器來記錄每次訪問實(shí)例屬性的嘗試:
def logger(cls): original_getattribute = cls.__getattribute__ def getattribute(self, name): print(f"Getting: '{name}'") return original_getattribute(self, name) cls.__getattribute__ = getattribute return cls @logger class SomeClass: def __init__(self, attr): self.attr = attr def func(self): ... instance = SomeClass("value") instance.attr # Getting: 'attr' instance.func() # Getting: 'func'
裝飾器函數(shù)logger 首先記錄它所裝飾的類的原始__getattribute__方法。然后將其替換為自定義方法,該方法在調(diào)用原始的__getattribute__方法之前記錄了被訪問屬性的名稱。
魔法屬性
到目前為止,我們只討論了魔法方法,但在Python中也有相當(dāng)多的魔法變量/屬性。其中一個(gè)是__all__:
# some_module/__init__.py __all__ = ["func", "some_var"] some_var = "data" some_other_var = "more data" def func(): return "hello" # ----------- from some_module import * print(some_var) # "data" print(func()) # "hello" print(some_other_var) # Exception, "some_other_var" is not exported by the module
這個(gè)屬性可用于定義從模塊導(dǎo)出哪些變量和函數(shù)。我們創(chuàng)建了一個(gè)Python模塊…/some_module/單獨(dú)文件(__init__.py)。在這個(gè)文件中定義了2個(gè)變量和一個(gè)函數(shù),只導(dǎo)出其中的2個(gè)(func和some_var)。如果我們嘗試在其他Python程序中導(dǎo)入some_module的內(nèi)容,我們只能得到2個(gè)內(nèi)容。
但是要注意,__all__變量只影響上面所示的* import,我們?nèi)匀豢梢允褂蔑@式的名稱導(dǎo)入函數(shù)和變量,比如import some_other_var from some_module。
另一個(gè)常見的雙下劃線變量(模塊屬性)是__file__。這個(gè)變量標(biāo)識(shí)了訪問它的文件的路徑:
from pathlib import Path print(__file__) print(Path(__file__).resolve()) # /home/.../directory/examples.py # Or the old way: import os print(os.path.dirname(os.path.abspath(__file__))) # /home/.../directory/
這樣我們就可以結(jié)合__all__和__file__,可以在一個(gè)文件夾中加載所有模塊:
# Directory structure: # . # |____some_dir # |____module_three.py # |____module_two.py # |____module_one.py from pathlib import Path, PurePath modules = list(Path(__file__).parent.glob("*.py")) print([PurePath(f).stem for f in modules if f.is_file() and not f.name == "__init__.py"]) # ['module_one', 'module_two', 'module_three']
最后一個(gè)我重要的屬性是的是__debug__。它可以用于調(diào)試,但更具體地說,它可以用于更好地控制斷言:
# example.py def func(): if __debug__: print("debugging logs") # Do stuff... func()
如果我們使用
python example.py
正常運(yùn)行這段代碼,我們將看到打印出“調(diào)試日志”,但是如果我們使用
python -O example.py
,優(yōu)化標(biāo)志(-O)將把__debug__設(shè)置為false并刪除調(diào)試消息。因此,如果在生產(chǎn)環(huán)境中使用-O運(yùn)行代碼,就不必?fù)?dān)心調(diào)試過程中被遺忘的打印調(diào)用,因?yàn)樗鼈兌疾粫?huì)顯示。
創(chuàng)建自己魔法方法
我們可以創(chuàng)建自己的方法和屬性嗎?是的,你可以,但你不應(yīng)該這么做。
雙下劃線名稱是為Python語言的未來擴(kuò)展保留的,不應(yīng)該用于自己的代碼。如果你決定在你的代碼中使用這樣的名稱,那么將來如果它們被添加到Python解釋器中,這就與你的代碼不兼容了。所以對(duì)于這些方法,我們只要記住和使用就好了。
到此這篇關(guān)于詳解Python中魔法方法的使用的文章就介紹到這了,更多相關(guān)Python魔法方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python調(diào)用函數(shù)、類和文件操作簡(jiǎn)單實(shí)例總結(jié)
這篇文章主要介紹了python調(diào)用函數(shù)、類和文件操作,結(jié)合簡(jiǎn)單實(shí)例形式總結(jié)分析了Python調(diào)用函數(shù)、類和文件操作的各種常見操作技巧,需要的朋友可以參考下2019-11-11Django2.1集成xadmin管理后臺(tái)所遇到的錯(cuò)誤集錦(填坑)
這篇文章主要介紹了Django2.1集成xadmin管理后臺(tái)所遇到的錯(cuò)誤集錦(填坑),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12python調(diào)用文件時(shí)找不到相對(duì)路徑的解決方案
這篇文章主要介紹了python調(diào)用文件時(shí)找不到相對(duì)路徑的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Python selenium 父子、兄弟、相鄰節(jié)點(diǎn)定位方式詳解
這篇文章主要介紹了Python selenium 父子、兄弟、相鄰節(jié)點(diǎn)定位方式詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Python?ModuleNotFoundError:?No?module?named?‘xxx‘可能的解決方
本文主要介紹了Python?ModuleNotFoundError:?No?module?named?‘xxx‘可能的解決方案大全,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧Chat?Gpt<BR>2023-07-07Python?pandas?DataFrame數(shù)據(jù)拼接方法
我們都知道在使用pandas處理數(shù)據(jù)的時(shí)候,往往會(huì)需要合并兩個(gè)或者多個(gè)DataFrame的操作,下面這篇文章主要給大家介紹了關(guān)于Python?pandas?DataFrame數(shù)據(jù)拼接方法的相關(guān)資料,需要的朋友可以參考下2022-07-07