用法:
class functools.partialmethod(func, /, *args, **keywords)返回一個新的
partialmethod說明符,它的行為類似於partial,隻是它被設計為用作方法定義而不是直接可調用。func必須是說明符或可調用對象(兩者都像普通函數一樣,作為說明符處理)。當
func是一個說明符(例如普通的 Python 函數、classmethod()、staticmethod()、abstractmethod()或partialmethod的另一個實例)時,對__get__的調用被委托給底層說明符,並且適當的作為結果返回的部分對象。當
func是非說明符可調用時,會動態創建適當的綁定方法。當用作方法時,它的行為類似於普通的 Python 函數:self參數將作為第一個位置參數插入,甚至在提供給partialmethod構造函數的args和keywords之前。例子:
>>> class Cell: ... def __init__(self): ... self._alive = False ... @property ... def alive(self): ... return self._alive ... def set_state(self, state): ... self._alive = bool(state) ... set_alive = partialmethod(set_state, True) ... set_dead = partialmethod(set_state, False) ... >>> c = Cell() >>> c.alive False >>> c.set_alive() >>> c.alive True3.4 版中的新函數。
相關用法
- Python functools.partial用法及代碼示例
- Python functools.wraps用法及代碼示例
- Python functools.singledispatchmethod用法及代碼示例
- Python functools.singledispatch用法及代碼示例
- Python functools.cache用法及代碼示例
- Python functools.lru_cache用法及代碼示例
- Python functools.reduce用法及代碼示例
- Python functools.cached_property用法及代碼示例
- Python functools.total_ordering用法及代碼示例
- Python functools.wraps()用法及代碼示例
- Python dict fromkeys()用法及代碼示例
- Python frexp()用法及代碼示例
- Python float轉exponential用法及代碼示例
- Python calendar firstweekday()用法及代碼示例
- Python fsum()用法及代碼示例
- Python float.is_integer用法及代碼示例
- Python format()用法及代碼示例
- Python calendar formatmonth()用法及代碼示例
- Python filecmp.cmpfiles()用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 functools.partialmethod。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
