This article mainly introduces the Functools module function parsing in Python, and explains the functools.cmp_to_key,functools.total_ordering, respectively. The use of Functools.reduce,functools.partial,functools.update_wrapper and functools.wraps, the need for friends can refer to the next
Python's own Functools module provides some of the most frequently used high-order functions, which are special functions for handling other functions. In other words, it is possible to use the module to process callable objects.
Functools Module Functions Overview
Functools.cmp_to_key (func)
Functools.total_ordering (CLS)
Functools.reduce (function, iterable[, initializer])
Functools.partial (func[, args][, *keywords])
Functools.update_wrapper (wrapper, wrapped[, assigned][, updated])
Functools.wraps (wrapped[, assigned][, updated])
Functools.cmp_to_key ()
Grammar:
functools.cmp_to_key(func)
This function is used to convert legacy comparison functions to keyword functions.
Legacy comparison function: receives two parameters and returns the result of the comparison. A return value of less than 0 is less than the latter, and the return value is greater than zero instead, and the return value equals zero is equal.
Keyword function: Receives a parameter that returns its corresponding comparable object. For example sorted (), Min (), Max (), Heapq.nlargest (), Heapq.nsmallest (), Itertools.groupby () are all available as keyword functions.
In Python 3, there are many places where legacy comparison functions are no longer supported and can be converted using Cmp_to_key ().
Example:
Sorted (iterable, Key=cmp_to_key (Cmp_func))
Functools.total_ordering ()
Grammar:
functools.total_ordering(cls)
This is a class decorator for automating the comparison of classes.
We only need to implement the EQ () method and any of the following methods in the Class (), Le (), GT (), GE (), then total_ordering () will automatically help us to achieve the remaining comparison operations.
Example:
@total_orderingclass 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 ()))
Functools.reduce ()
Grammar:
functools.reduce(function, iterable[, initializer])
The function is the same as the python built-in reduce () function, which is primarily used to write code that is compatible with Python 3.
Functools.partial ()
Grammar:
functools.partial(func[, *args][, **keywords])
The function returns a partial object that invokes the Func function, and passes in the positional parameter args and the keyword argument keywords. If a positional parameter is passed in when the object is called, the parameters are added to args. If the keyword argument is passed in, it is added to the keywords.
The equivalent implementation of the partial () function is roughly as follows:
def partial (func, *args, **keywords): def newfunc (*fargs, **fkeywords): newkeywords = keywords.copy () Newkeywords.update (fkeywords) return func (* (args + Fargs), **newkeywords) Newfunc.func = Func Newfunc.args = args newfunc.keywords = keywords return newfunc
The partial () function is primarily used to "freeze" some of the parameters of a function, returning a function object with fewer parameters and simpler use.
Example:
>>> from functools import partial>>> basetwo = partial (int, base=2) >>> Basetwo.doc = ' Convert b ASE 2 string to an int. ' >>> basetwo (' 10010 ') 18
Functools.update_wrapper ()
Grammar:
Functools.update_wrapper (wrapper, wrapped[, assigned][, updated])
This function is used to update the wrapper function (wrapper) so that it looks like the original function. The optional parameter is a tuple, and the assigned tuple specifies the property to be replaced directly with the value of the original function, and the updated tuple specifies the property to be updated against the original function. The default values for these two parameters are module-level constants: Wrapper_assignments and Wrapper_updates. The former specifies the name, module, and Doc properties of the wrapper function to be directly assigned, and the latter specifies the Dict property of the wrapper function to be updated.
This function is primarily used in the definition of the adorner function before it is placed in the wrapper function. If the wrapper function is not updated, the meta-information of the decorated function becomes the meta-information of the wrapper function, not the meta-information of the original function.
Functools.wraps ()
Grammar:
Functools.wraps (wrapped[, assigned][, updated])
Wraps () simplifies the invocation of the Update_wrapper () function. It is equivalent to partial (Update_wrapper, wrapped=wrapped, Assigned, updated=updated).
Example:
>>> 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 >>> exam Ple.name ' Example ' >>> example.doc ' docstring '
If you do not use this function, the function name in the example becomes wrapper, and the description document (docstring) of the original function example () is lost.