functools
— 可调用对象的高阶函数和操作
¶
源代码: Lib/functools.py
The
functools
模块用于高阶函数:充当 (或返回) 其它函数的函数。一般而言,任何可调用对象都可以视为用于此模块用途的函数。
The
functools
模块定义了下列函数:
@
functools.
cached_property
(
func
)
¶
将类方法变换成值计算一次,然后将值作为实例生命周期正常缓存属性的属性。类似于
property()
, with the addition of caching. Useful for expensive computed properties of instances that are otherwise effectively immutable.
范例:
class DataSet: def __init__(self, sequence_of_numbers): self._data = sequence_of_numbers @cached_property def stdev(self): return statistics.stdev(self._data) @cached_property def variance(self): return statistics.variance(self._data)
3.8 版新增。
注意
This decorator requires that the
__dict__
attribute on each instance be a mutable mapping. This means it will not work with some types, such as metaclasses (since the
__dict__
attributes on type instances are read-only proxies for the class namespace), and those that specify
__slots__
without including
__dict__
as one of the defined slots (as such classes don’t provide a
__dict__
attribute at all).
functools.
cmp_to_key
(
func
)
¶
Transform an old-style comparison function to a
关键函数
. Used with tools that accept key functions (such as
sorted()
,
min()
,
max()
,
heapq.nlargest()
,
heapq.nsmallest()
,
itertools.groupby()
). This function is primarily used as a transition tool for programs being converted from Python 2 which supported the use of comparison functions.
A comparison function is any callable that accept two arguments, compares them, and returns a negative number for less-than, zero for equality, or a positive number for greater-than. A key function is a callable that accepts one argument and returns another value to be used as the sort key.
范例:
sorted(iterable, key=cmp_to_key(locale.strcoll)) # locale-aware sort order
对于排序范例和简短排序教程,见 排序怎么样 .
3.2 版新增。
@
functools.
lru_cache
(
user_function
)
¶
@
functools.
lru_cache
(
maxsize=128
,
typed=False
)
装饰器采用保存的记忆可调用来包裹函数,直到 maxsize most recent calls. It can save time when an expensive or I/O bound function is periodically called with the same arguments.
Since a dictionary is used to cache results, the positional and keyword arguments to the function must be hashable.
Distinct argument patterns may be considered to be distinct calls with separate cache entries. For example, f(a=1, b=2) and f(b=2, a=1) differ in their keyword argument order and may have two separate cache entries.
若 user_function 有指定,它必须是可调用。这允许 lru_cache decorator to be applied directly to a user function, leaving the maxsize at its default value of 128:
@lru_cache def count_vowels(sentence): sentence = sentence.casefold() return sum(sentence.count(vowel) for vowel in 'aeiou')
若
maxsize
被设为
None
, the LRU feature is disabled and the cache can grow without bound.
若
typed
is set to true, function arguments of different types will be cached separately. For example,
f(3)
and
f(3.0)
will be treated as distinct calls with distinct results.
To help measure the effectiveness of the cache and tune the
maxsize
parameter, the wrapped function is instrumented with a
cache_info()
function that returns a
命名元组
showing
hits
,
misses
,
maxsize
and
currsize
. In a multi-threaded environment, the hits and misses are approximate.
装饰器还提供
cache_clear()
函数用于清零 (或使缓存无效)。
The original underlying function is accessible through the
__wrapped__
attribute. This is useful for introspection, for bypassing the cache, or for rewrapping the function with a different cache.
An LRU (least recently used) cache works best when the most recent calls are the best predictors of upcoming calls (for example, the most popular articles on a news server tend to change each day). The cache’s size limit assures that the cache does not grow without bound on long-running processes such as web servers.
In general, the LRU cache should only be used when you want to reuse previously computed values. Accordingly, it doesn’t make sense to cache functions with side-effects, functions that need to create distinct mutable objects on each call, or impure functions such as time() or random().
Example of an LRU cache for static web content:
@lru_cache(maxsize=32) def get_pep(num): 'Retrieve text of a Python Enhancement Proposal' resource = 'http://www.python.org/dev/peps/pep-%04d/' % num try: with urllib.request.urlopen(resource) as s: return s.read() except urllib.error.HTTPError: return 'Not Found' >>> for n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991: ... pep = get_pep(n) ... print(n, len(pep)) >>> get_pep.cache_info() CacheInfo(hits=3, misses=8, maxsize=32, currsize=8)
Example of efficiently computing 斐波那契数 using a cache to implement a 动态编程 技术:
@lru_cache(maxsize=None) def fib(n): if n < 2: return n return fib(n-1) + fib(n-2) >>> [fib(n) for n in range(16)] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] >>> fib.cache_info() CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)
3.2 版新增。
3.3 版改变: 添加 typed 选项。
3.8 版改变: 添加 user_function 选项。
@
functools.
total_ordering
¶
给定定义一个或多个丰富比较次序方法的类,此类装饰器供给其余方法。这简化指定所有可能丰富比较操作涉及的努力:
类必须定义一个
__lt__()
,
__le__()
,
__gt__()
,或
__ge__()
。此外,类应提供
__eq__()
方法。
例如:
@total_ordering class Student: def _is_valid_operand(self, other): return (hasattr(other, "lastname") and hasattr(other, "firstname")) def __eq__(self, other): if not self._is_valid_operand(other): return NotImplemented return ((self.lastname.lower(), self.firstname.lower()) == (other.lastname.lower(), other.firstname.lower())) def __lt__(self, other): if not self._is_valid_operand(other): return NotImplemented return ((self.lastname.lower(), self.firstname.lower()) < (other.lastname.lower(), other.firstname.lower()))
注意
While this decorator makes it easy to create well behaved totally ordered types, it does come at the cost of slower execution and more complex stack traces for the derived comparison methods. If performance benchmarking indicates this is a bottleneck for a given application, implementing all six rich comparison methods instead is likely to provide an easy speed boost.
3.2 版新增。
3.4 版改变: Returning NotImplemented from the underlying comparison function for unrecognised types is now supported.
functools.
partial
(
func
,
/
,
*args
,
**keywords
)
¶
返回新的 partial 对象 which when called will behave like func called with the positional arguments args 和关键词自变量 keywords . If more arguments are supplied to the call, they are appended to args . If additional keyword arguments are supplied, they extend and override keywords . Roughly equivalent to:
def partial(func, /, *args, **keywords): def newfunc(*fargs, **fkeywords): newkeywords = {**keywords, **fkeywords} return func(*args, *fargs, **newkeywords) newfunc.func = func newfunc.args = args newfunc.keywords = keywords return newfunc
The
partial()
is used for partial function application which “freezes” some portion of a function’s arguments and/or keywords resulting in a new object with a simplified signature. For example,
partial()
can be used to create a callable that behaves like the
int()
function where the
base
argument defaults to two:
>>> from functools import partial >>> basetwo = partial(int, base=2) >>> basetwo.__doc__ = 'Convert base 2 string to an int.' >>> basetwo('10010') 18
functools.
partialmethod
(
func
,
/
,
*args
,
**keywords
)
¶
返回新的
partialmethod
描述符其行为像
partial
except that it is designed to be used as a method definition rather than being directly callable.
func 必须为 descriptor or a callable (objects which are both, like normal functions, are handled as descriptors).
当
func
是描述符 (譬如:正常 Python 函数,
classmethod()
,
staticmethod()
,
abstractmethod()
或另一实例化的
partialmethod
),调用
__get__
are delegated to the underlying descriptor, and an appropriate
partial 对象
returned as the result.
当
func
is a non-descriptor callable, an appropriate bound method is created dynamically. This behaves like a normal Python function when used as a method: the
self
argument will be inserted as the first positional argument, even before the
args
and
keywords
supplied to the
partialmethod
构造函数。
范例:
>>> class Cell(object): ... 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 True
3.4 版新增。
functools.
reduce
(
function
,
iterable
[
,
initializer
]
)
¶
应用
function
的 2 自变量的累积到项为
iterable
,从左到右,以便将可迭代缩减到单个值。例如,
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
计算
((((1+2)+3)+4)+5)
。左自变量
x
, is the accumulated value and the right argument,
y
, is the update value from the
iterable
. If the optional
initializer
is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If
initializer
未给定且
iterable
contains only one item, the first item is returned.
大致相当于:
def reduce(function, iterable, initializer=None): it = iter(iterable) if initializer is None: value = next(it) else: value = initializer for element in it: value = function(value, element) return value
见
itertools.accumulate()
for an iterator that yields all intermediate values.
@
functools.
singledispatch
¶
To define a generic function, decorate it with the
@singledispatch
decorator. Note that the dispatch happens on the type of the first argument, create your function accordingly:
>>> from functools import singledispatch >>> @singledispatch ... def fun(arg, verbose=False): ... if verbose: ... print("Let me just say,", end=" ") ... print(arg)
To add overloaded implementations to the function, use the
register()
attribute of the generic function. It is a decorator. For functions annotated with types, the decorator will infer the type of the first argument automatically:
>>> @fun.register ... def _(arg: int, verbose=False): ... if verbose: ... print("Strength in numbers, eh?", end=" ") ... print(arg) ... >>> @fun.register ... def _(arg: list, verbose=False): ... if verbose: ... print("Enumerate this:") ... for i, elem in enumerate(arg): ... print(i, elem)
For code which doesn’t use type annotations, the appropriate type argument can be passed explicitly to the decorator itself:
>>> @fun.register(complex) ... def _(arg, verbose=False): ... if verbose: ... print("Better than complicated.", end=" ") ... print(arg.real, arg.imag) ...
To enable registering lambdas and pre-existing functions, the
register()
attribute can be used in a functional form:
>>> def nothing(arg, verbose=False): ... print("Nothing.") ... >>> fun.register(type(None), nothing)
The
register()
attribute returns the undecorated function which enables decorator stacking, pickling, as well as creating unit tests for each variant independently:
>>> @fun.register(float) ... @fun.register(Decimal) ... def fun_num(arg, verbose=False): ... if verbose: ... print("Half of your number:", end=" ") ... print(arg / 2) ... >>> fun_num is fun False
When called, the generic function dispatches on the type of the first argument:
>>> fun("Hello, world.") Hello, world. >>> fun("test.", verbose=True) Let me just say, test. >>> fun(42, verbose=True) Strength in numbers, eh? 42 >>> fun(['spam', 'spam', 'eggs', 'spam'], verbose=True) Enumerate this: 0 spam 1 spam 2 eggs 3 spam >>> fun(None) Nothing. >>> fun(1.23) 0.615
Where there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation. The original function decorated with
@singledispatch
is registered for the base
object
type, which means it is used if no better implementation is found.
To check which implementation will the generic function choose for a given type, use the
dispatch()
属性:
>>> fun.dispatch(float) <function fun_num at 0x1035a2840> >>> fun.dispatch(dict) # note: default implementation <function fun at 0x103fe0000>
To access all registered implementations, use the read-only
registry
属性:
>>> fun.registry.keys() dict_keys([<class 'NoneType'>, <class 'int'>, <class 'object'>, <class 'decimal.Decimal'>, <class 'list'>, <class 'float'>]) >>> fun.registry[float] <function fun_num at 0x1035a2840> >>> fun.registry[object] <function fun at 0x103fe0000>
3.4 版新增。
3.7 版改变:
The
register()
属性支持使用类型注解。
functools.
singledispatchmethod
(
func
)
¶
要定义一般方法,装饰它采用
@singledispatchmethod
decorator. Note that the dispatch happens on the type of the first non-self or non-cls argument, create your function accordingly:
class Negator: @singledispatchmethod def neg(self, arg): raise NotImplementedError("Cannot negate a") @neg.register def _(self, arg: int): return -arg @neg.register def _(self, arg: bool): return not arg
@singledispatchmethod
supports nesting with other decorators such as
@classmethod
. Note that to allow for
dispatcher.register
,
singledispatchmethod
must be the
outer most
decorator. Here is the
Negator
类采用
neg
methods being class bound:
class Negator: @singledispatchmethod @classmethod def neg(cls, arg): raise NotImplementedError("Cannot negate a") @neg.register @classmethod def _(cls, arg: int): return -arg @neg.register @classmethod def _(cls, arg: bool): return not arg
The same pattern can be used for other similar decorators:
staticmethod
,
abstractmethod
, and others.
3.8 版新增。
functools.
update_wrapper
(
wrapper
,
wrapped
,
assigned=WRAPPER_ASSIGNMENTS
,
updated=WRAPPER_UPDATES
)
¶
Update a
wrapper
function to look like the
wrapped
function. The optional arguments are tuples to specify which attributes of the original function are assigned directly to the matching attributes on the wrapper function and which attributes of the wrapper function are updated with the corresponding attributes from the original function. The default values for these arguments are the module level constants
WRAPPER_ASSIGNMENTS
(which assigns to the wrapper function’s
__module__
,
__name__
,
__qualname__
,
__annotations__
and
__doc__
, the documentation string) and
WRAPPER_UPDATES
(which updates the wrapper function’s
__dict__
, i.e. the instance dictionary).
To allow access to the original function for introspection and other purposes (e.g. bypassing a caching decorator such as
lru_cache()
), this function automatically adds a
__wrapped__
attribute to the wrapper that refers to the function being wrapped.
The main intended use for this function is in 装饰器 functions which wrap the decorated function and return the wrapper. If the wrapper function is not updated, the metadata of the returned function will reflect the wrapper definition rather than the original function definition, which is typically less than helpful.
update_wrapper()
may be used with callables other than functions. Any attributes named in
assigned
or
updated
that are missing from the object being wrapped are ignored (i.e. this function will not attempt to set them on the wrapper function).
AttributeError
is still raised if the wrapper function itself is missing any attributes named in
updated
.
3.2 版新增:
自动附加
__wrapped__
属性。
3.2 版新增:
Copying of the
__annotations__
attribute by default.
3.2 版改变:
缺失属性不再触发
AttributeError
.
3.4 版改变:
The
__wrapped__
attribute now always refers to the wrapped function, even if that function defined a
__wrapped__
属性。(见
bpo-17482
)
@
functools.
wraps
(
wrapped
,
assigned=WRAPPER_ASSIGNMENTS
,
updated=WRAPPER_UPDATES
)
¶
这是方便函数为援引
update_wrapper()
作为函数装饰器当定义包裹器函数时。它相当于
partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)
。例如:
>>> from functools import wraps >>> def my_decorator(f): ... @wraps(f) ... def wrapper(*args, **kwds): ... print('Calling decorated function') ... return f(*args, **kwds) ... return wrapper ... >>> @my_decorator ... def example(): ... """Docstring""" ... print('Called example function') ... >>> example() Calling decorated function Called example function >>> example.__name__ 'example' >>> example.__doc__ 'Docstring'
不使用此装饰器工厂,example 函数名称将拥有
'wrapper'
,且 docstring (文档字符串) 对于原始
example()
会丢失。
partial
对象
¶
partial
对象是可调用对象创建通过
partial()
。它们有 3 个只读属性:
partial.
args
¶
The leftmost positional arguments that will be prepended to the positional arguments provided to a
partial
object call.
partial.
keywords
¶
The keyword arguments that will be supplied when the
partial
object is called.
partial
对象像
function
objects in that they are callable, weak referencable, and can have attributes. There are some important differences. For instance, the
__name__
and
__doc__
attributes are not created automatically. Also,
partial
objects defined in classes behave like static methods and do not transform into bound methods during instance attribute look-up.