Chapter 15. Data Structures, Algorithms, and Code Simplification¶
abc
¶
array
¶
bisect
¶
collections
¶
contextlib
¶
functools
¶
The functools
module contains functions and decorators that are useful for creating higher-order functions, functional programming, and decorators.
partial()
*¶
partial(function [, *args [, **kwargs]])
The partial()
function creates a function-like object, partial
. When called, the partial
object calls function with positional arguments args, keyword arguments kwargs, and any additional positional or keyword arguments that are supplied. Additional positional arguments are added to the end of args, and additional keyword arguments are merged into kwargs, overwriting any previously defined values (if any).
A typical use of partial()
is when making a large number of function calls where many of the arguments are held fixed. For example:
from functools import partial mybutton = partial(Button, root, fg="black", bg="white", font="times", size="12") b1 = mybutton(text="Ok") # Calls Button() with text="Ok" and all of the b2 = mybutton(text="Cancel") # additional arguments supplied to partial() above b3 = mybutton(text="Restart")
A partial
instance p
has the following attributes:
Item | Description |
---|---|
p.func |
Function that is called when p is called. |
p.args |
Tuple containing the leftmost positional arguments supplied to p.func when called. Additional positional arguments are concatenated to the end of this value. |
p.keywords |
Dictionary containing the keyword arguments supplied to p.func when called. Additional keyword arguments are merged into this dictionary. |
Note that a partial
object does not behave exactly the same as a regular function. For instance, if you use partial()
inside a class definition, it behaves like a static method, not an instance method. [p268]
reduce()
*¶
reduce(function, items [, initial])
This function is the same as the reduce()
function that was a built-in in Python 2. For future compatibility, use this version instead.
update_wrapper()
*¶
update_wrapper(wrapper, wrapped [, assigned [, updated]])
update_wrapper()
is a utility function for writing decorators. It copies attributes from a function wrapped to a wrapper function wrapper in order to make the wrapped function look like the original function.
- assigned is a tuple of attribute names to copy and is set to
('__name__','__module__','__doc__')
by default. - updated is a tuple containing the names of function attributes that are dictionaries and which you want values merged in the wrapper. By default, it is a tuple
('__dict__',)
.
wraps()
*¶
wraps(function [, assigned [, updated ]])
wraps()
is a decorator which carries out the same task as update_wrapper()
on the function to which it is applied. A typical use of this decorator is when writing other decorators. For example:
from functools import wraps def debug(func): @wraps(func) def wrapped(*args,**kwargs): print("Calling %s" % func.__name_) r = func(*args,**kwargs) print("Done calling %s" % func._name__) return wrapped @debug def add(x,y): return x+y