The module provides support for higher-order functions-functions that act on or return functions are called higher-order functions. It appears to the module that all callable objects can be considered as "functions" in this module.
Directory
First, the module method
1. Functools.cmp_to_key (func)
2. functools.total_ordering (CLS)
3. Functools.reduce (function, iterable[, initializer])
. Functools.partial (func[,*args][, **keywords])
5. Functools.update_wrapper (wrapper, wrapped[, assigned][, updated])
*6. Functools.wraps (wrapped[, assigned][, updated])
Second, partial object
1. Partial.func
2. Partial.args
3. Partial.keywords
First, the module method
The following methods are defined in the module:
1.
Functools.cmp_to_key (func)
Convert old-fashioned comparison functions (comparison function) into keyword functions (key function). Use with tools that accept key function (such as sorted (), Min (), Max (), Heapq.nlargest (), Itertools.groupby ()). This function is mainly used to convert the program to Python 3 format, because the comparison function is not supported in Python 3.
The comparison function is callable, takes two parameters, compares the two parameters, and returns a negative value, 0, or one of the positive values based on their size relationship. The keyword function is also callable, takes a parameter, and returns a value that can be used as the sort key.
For example:
Sorted (iterable, Key=cmp_to_key (Locale.strcoll))
2.
functools.total_ordering (CLS)
This is a class decorator, given a class that defines one or more comparison sorting methods, which will complement the rest of the comparison methods, reducing the amount of work that is required to define all the comparison methods.
The modified class must define at least one of the __lt__ (), __le__ (), __gt__ (), or __ge__ (), and the modified class should also provide the __eq__ () method.
For example:
@total_ordering class Student: def __eq__ (self, Other): return ((Self.lastname.lower (), self.firstname.lower ()) = = (Other.lastname.lower (), Other.firstname.lower ())) def __lt__ (self, Other): return ((Self.lastname.lower (), Self.firstname.lower ()) < (Other.lastname.lower (), Other.firstname.lower ()) )
3.
functools.reduce (function, iterable[, initializer])
The same as the reduce () method, which provides forward support for the transition to Python 3.
4.
functools.partial (func[,*args][, **keywords])
The function adorner, which returns a new partial object, is described below for a partial object. Calling the partial object is the same as calling the decorated function func, except that the number of arguments passed in when the partial object is called is usually less than the number of arguments passed in when the Func is called. When a function func can receive a number of parameters, and one use only needs to change some of the parameters, the partial object can freeze these immutable objects when they are called, so that when the partial object is invoked, the partial Object invokes Func, along with the parameters that have been frozen, to the Func function, simplifying the invocation process.
If more arguments are provided when the partial object is called, they will be appended to the args, and if more keyword arguments are provided, they will extend or overwrite the frozen keyword arguments
The partial object functions as follows:
def Partial (func, *args, * *keywords) :def newfunc (*fargs, * *fkeywords): = keywords.copy () newkeywords.update (fkeywords) return func (* (args + Fargs), * * newkeywords) = func = args = keywords return Newfunc
Create an int () with a base parameter that is always 2 using the partial object
from Import = partial (int, base=2) basetwo. __doc__ ' Convert Base 2 string to an int. ' basetwo ('10010')
This new partial object, Basetwo, is able to convert binary parameters into decimal integer results, which only needs to pass in the binary target parameters when calling this partial object.
5.
Functools.update_wrapper (Wrapper, wrapped[, assigned][, updated])
Update a parcel (wrapper) function so that it looks more like a function that is wrapped (wrapped).
The optional parameter specifies which attributes of the wrapped function are assigned directly to the corresponding property of the package function, and which properties of the wrapping function are updated rather than directly accepting the corresponding property of the wrapped function. The default value of the parameter assigned corresponds to the module-level constant wrapper_assignments (by default the __name__, __module__, and __doc__ attributes of the wrapped function are assigned to the Wrap function), and the parameter Updatedd The default value corresponds to the module-level constant wrapper_updates (The __dict__ property of the default update WRAPPER function).
The main purpose of this function is in an adorner, the original function will be decorated (wrapped), the adorner function will return a wrapper function, if the adorner returned by the wrapper function is not updated, then some of its metadata is more reflective of the wrapper function definition when the characteristics, can not reflect The properties of the wrapped function.
6.
functools.wraps (wrapped[, assigned][, updated])
This function can be used as an adorner to simplify the process of calling the previous function Update_wrapper. Calling this function is equivalent to calling partial (Update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated) .
For example:
>>> fromFunctoolsImportWraps>>>defMy_decorator (f): ... @wraps (f) ...defWrapper (*args, * *kwds): ...Print 'Calling decorated function'... returnF (*args, * *kwds) ...returnwrapper ...>>>@my_decorator ...defexample (): ..."""docstring"""... Print 'called example function'...>>>example () calling decorated functioncalled example function>>> example.__name__'Example'>>> example.__doc__'docstring'
You can see that the final call to the example () function is @my_decorator decorated, and the function of the adorner is to accept a wrapped function as a parameter, process it, and return a wrapping function. The code uses the @functools. Wraps decoration will return the package function wrapper, making his __name__, __doc__ and __module__ attributes exactly the same as the decorated function examle (), so that although the final call is decorated exam Ple () function, but some of the properties are still being maintained.
If @functools is not used in the definition of the @my_decorator. Wraps decorate the wrapping function, then the final example.__name__ will become ' wrapper ', and example.__doc__ will be lost.
Partial object
The partial object is a callable object created when you call partial (), and they have three read-only properties:
1. Partial.func
Callable or function, when a partial object is called, the Func is eventually called with the new parameters and keywords.
2. Partial.args
The default is the leftmost positional parameter, which is automatically added to all parameters that are passed in when the partial object is called. That is, you do not have to pass in these parameters when you call the partial object, they are considered constant and are automatically added to the call to Func.
3. Partial.keywords
The keyword argument that is supplied when the partial object is called.
Partial objects are similar to function objects in some ways: callable, weak references, and can have properties. But there are some key differences:
The __name__ and __doc__ properties of 1.partial objects are not created automatically;
2. A partial object defined in a class is used more like a static method, and is not transformed into a binding method (bound methods, a property reference through an instance object of a class) during an instance's property query.
Python--functools