9.8. functools — Funções e operações de ordem superior em objetos chamáveis

Novo na versão 2.5.

** Código fonte: **: source:Lib/functools.py

O módulo: mod: functools é para funções de ordem superior: funções que atuam ou retornam outras funções. Em geral, qualquer objeto invocável pode ser tratado como uma função para os propósitos deste módulo.

O módulo: mod:functools define as seguintes funções:


Transform an old-style comparison function to a key function. 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 to Python 3 where comparison functions are no longer supported.

Uma função de comparação é qualquer chamada que aceita dois argumentos, os compara e retorna um número negativo por menos de zero, igual a igualdade ou um número positivo por maior que. Uma função de chave é escalável que aceita um argumento e retorna outro valor para ser usado como a chave de classificação.


sorted(iterable, key=cmp_to_key(locale.strcoll))  # locale-aware sort order

Para selecionar exemplos e um breve tutorial de classificação, veja: ref: sortinghowto.

Novo na versão 2.7.


Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest. This simplifies the effort involved in specifying all of the possible rich comparison operations:

The class must define one of __lt__(), __le__(), __gt__(), or __ge__(). In addition, the class should supply an __eq__() method.

Por exemplo:

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()))

Novo na versão 2.7.

functools.reduce(function, iterable[, initializer])

This is the same function as reduce(). It is made available in this module to allow writing code more forward-compatible with Python 3.

Novo na versão 2.6.

functools.partial(func[,*args][, **keywords])

Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments 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.copy()
        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')
functools.update_wrapper(wrapper, wrapped[, assigned][, updated])

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 __name__, __module__ and __doc__, the documentation string) and WRAPPER_UPDATES (which updates the wrapper function’s __dict__, i.e. the instance dictionary).

The main intended use for this function is in decorator 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.

functools.wraps(wrapped[, assigned][, updated])

This is a convenience function for invoking update_wrapper() as a function decorator when defining a wrapper function. It is equivalent to partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated). For 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
>>> example.__name__
>>> example.__doc__

Without the use of this decorator factory, the name of the example function would have been 'wrapper', and the docstring of the original example() would have been lost.

9.8.1. Objetos partial

partial objects are callable objects created by partial(). They have three read-only attributes:


A callable object or function. Calls to the partial object will be forwarded to func with new arguments and keywords.


The leftmost positional arguments that will be prepended to the positional arguments provided to a partial object call.


The keyword arguments that will be supplied when the partial object is called.

partial objects are like 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.