functools API Reference

Full documentation for functools (Standard Library / Native)

functools.GenericAlias
Python Library Documentation: class GenericAlias in module types

class GenericAlias(builtins.object)
 |  Represent a PEP 585 generic type
 |
 |  E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).
 |
 |  Methods defined here:
 |
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.
 |
 |  __dir__(...)
 |      Default dir() implementation.
 |
 |  __eq__(self, value, /)
 |      Return self==value.
 |
 |  __ge__(self, value, /)
 |      Return self>=value.
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __getitem__(self, key, /)
 |      Return self[key].
 |
 |  __gt__(self, value, /)
 |      Return self>value.
 |
 |  __hash__(self, /)
 |      Return hash(self).
 |
 |  __instancecheck__(...)
 |      Check if an object is an instance.
 |
 |  __iter__(self, /)
 |      Implement iter(self).
 |
 |  __le__(self, value, /)
 |      Return self<=value.
 |
 |  __lt__(self, value, /)
 |      Return self<value.
 |
 |  __mro_entries__(...)
 |
 |  __ne__(self, value, /)
 |      Return self!=value.
 |
 |  __or__(self, value, /)
 |      Return self|value.
 |
 |  __reduce__(...)
 |      Helper for pickle.
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __ror__(self, value, /)
 |      Return value|self.
 |
 |  __subclasscheck__(...)
 |      Check if a class is a subclass.
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs)
 |      Create and return a new object.  See help(type) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __args__
 |
 |  __origin__
 |
 |  __parameters__
 |      Type variables in the GenericAlias.
 |
 |  __typing_unpacked_tuple_args__
 |
 |  __unpacked__
functools.RLock
Python Library Documentation: class RLock in module _thread

class RLock(builtins.object)
 |  Methods defined here:
 |
 |  __enter__(...)
 |      acquire(blocking=True) -> bool
 |
 |      Lock the lock.  `blocking` indicates whether we should wait
 |      for the lock to be available or not.  If `blocking` is False
 |      and another thread holds the lock, the method will return False
 |      immediately.  If `blocking` is True and another thread holds
 |      the lock, the method will wait for the lock to be released,
 |      take it and then return True.
 |      (note: the blocking operation is interruptible.)
 |
 |      In all other cases, the method will return True immediately.
 |      Precisely, if the current thread already holds the lock, its
 |      internal counter is simply incremented. If nobody holds the lock,
 |      the lock is taken and its internal counter initialized to 1.
 |
 |  __exit__(...)
 |      release()
 |
 |      Release the lock, allowing another thread that is blocked waiting for
 |      the lock to acquire the lock.  The lock must be in the locked state,
 |      and must be locked by the same thread that unlocks it; otherwise a
 |      `RuntimeError` is raised.
 |
 |      Do note that if the lock was acquire()d several times in a row by the
 |      current thread, release() needs to be called as many times for the lock
 |      to be available for other threads.
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  acquire(...)
 |      acquire(blocking=True) -> bool
 |
 |      Lock the lock.  `blocking` indicates whether we should wait
 |      for the lock to be available or not.  If `blocking` is False
 |      and another thread holds the lock, the method will return False
 |      immediately.  If `blocking` is True and another thread holds
 |      the lock, the method will wait for the lock to be released,
 |      take it and then return True.
 |      (note: the blocking operation is interruptible.)
 |
 |      In all other cases, the method will return True immediately.
 |      Precisely, if the current thread already holds the lock, its
 |      internal counter is simply incremented. If nobody holds the lock,
 |      the lock is taken and its internal counter initialized to 1.
 |
 |  release(...)
 |      release()
 |
 |      Release the lock, allowing another thread that is blocked waiting for
 |      the lock to acquire the lock.  The lock must be in the locked state,
 |      and must be locked by the same thread that unlocks it; otherwise a
 |      `RuntimeError` is raised.
 |
 |      Do note that if the lock was acquire()d several times in a row by the
 |      current thread, release() needs to be called as many times for the lock
 |      to be available for other threads.
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs)
 |      Create and return a new object.  See help(type) for accurate signature.
functools.WRAPPER_ASSIGNMENTS
Python Library Documentation: tuple object

class tuple(object)
 |  tuple(iterable=(), /)
 |
 |  Built-in immutable sequence.
 |
 |  If no argument is given, the constructor returns an empty tuple.
 |  If iterable is specified the tuple is initialized from iterable's items.
 |
 |  If the argument is a tuple, the return value is the same object.
 |
 |  Built-in subclasses:
 |      asyncgen_hooks
 |      UnraisableHookArgs
 |
 |  Methods defined here:
 |
 |  __add__(self, value, /)
 |      Return self+value.
 |
 |  __contains__(self, key, /)
 |      Return bool(key in self).
 |
 |  __eq__(self, value, /)
 |      Return self==value.
 |
 |  __ge__(self, value, /)
 |      Return self>=value.
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __getitem__(self, key, /)
 |      Return self[key].
 |
 |  __getnewargs__(self, /)
 |
 |  __gt__(self, value, /)
 |      Return self>value.
 |
 |  __hash__(self, /)
 |      Return hash(self).
 |
 |  __iter__(self, /)
 |      Implement iter(self).
 |
 |  __le__(self, value, /)
 |      Return self<=value.
 |
 |  __len__(self, /)
 |      Return len(self).
 |
 |  __lt__(self, value, /)
 |      Return self<value.
 |
 |  __mul__(self, value, /)
 |      Return self*value.
 |
 |  __ne__(self, value, /)
 |      Return self!=value.
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __rmul__(self, value, /)
 |      Return value*self.
 |
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |
 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.
 |
 |      Raises ValueError if the value is not present.
 |
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |
 |  __class_getitem__(...)
 |      See PEP 585
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs)
 |      Create and return a new object.  See help(type) for accurate signature.
functools.WRAPPER_UPDATES
Python Library Documentation: tuple object

class tuple(object)
 |  tuple(iterable=(), /)
 |
 |  Built-in immutable sequence.
 |
 |  If no argument is given, the constructor returns an empty tuple.
 |  If iterable is specified the tuple is initialized from iterable's items.
 |
 |  If the argument is a tuple, the return value is the same object.
 |
 |  Built-in subclasses:
 |      asyncgen_hooks
 |      UnraisableHookArgs
 |
 |  Methods defined here:
 |
 |  __add__(self, value, /)
 |      Return self+value.
 |
 |  __contains__(self, key, /)
 |      Return bool(key in self).
 |
 |  __eq__(self, value, /)
 |      Return self==value.
 |
 |  __ge__(self, value, /)
 |      Return self>=value.
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __getitem__(self, key, /)
 |      Return self[key].
 |
 |  __getnewargs__(self, /)
 |
 |  __gt__(self, value, /)
 |      Return self>value.
 |
 |  __hash__(self, /)
 |      Return hash(self).
 |
 |  __iter__(self, /)
 |      Implement iter(self).
 |
 |  __le__(self, value, /)
 |      Return self<=value.
 |
 |  __len__(self, /)
 |      Return len(self).
 |
 |  __lt__(self, value, /)
 |      Return self<value.
 |
 |  __mul__(self, value, /)
 |      Return self*value.
 |
 |  __ne__(self, value, /)
 |      Return self!=value.
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __rmul__(self, value, /)
 |      Return value*self.
 |
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |
 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.
 |
 |      Raises ValueError if the value is not present.
 |
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |
 |  __class_getitem__(...)
 |      See PEP 585
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs)
 |      Create and return a new object.  See help(type) for accurate signature.
functools.cache
Python Library Documentation: function cache in module functools

cache(user_function, /)
    Simple lightweight unbounded cache.  Sometimes called "memoize".
functools.cached_property
Python Library Documentation: class cached_property in module functools

class cached_property(builtins.object)
 |  cached_property(func)
 |
 |  Methods defined here:
 |
 |  __get__(self, instance, owner=None)
 |
 |  __init__(self, func)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  __set_name__(self, owner, name)
 |
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |
 |  __class_getitem__ = GenericAlias(...)
 |      Represent a PEP 585 generic type
 |
 |      E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables
 |
 |  __weakref__
 |      list of weak references to the object
functools.cmp_to_key
Python Library Documentation: built-in function cmp_to_key in module _functools

cmp_to_key(mycmp)
    Convert a cmp= function into a key= function.

    mycmp
      Function that compares two objects.
functools.get_cache_token
Python Library Documentation: built-in function get_cache_token in module _abc

get_cache_token()
    Returns the current ABC cache token.

    The token is an opaque object (supporting equality testing) identifying the
    current version of the ABC cache for virtual subclasses. The token changes
    with every call to register() on any ABC.
functools.lru_cache
Python Library Documentation: function lru_cache in module functools

lru_cache(maxsize=128, typed=False)
    Least-recently-used cache decorator.

    If *maxsize* is set to None, the LRU features are disabled and the cache
    can grow without bound.

    If *typed* is True, arguments of different types will be cached separately.
    For example, f(3.0) and f(3) will be treated as distinct calls with
    distinct results.

    Arguments to the cached function must be hashable.

    View the cache statistics named tuple (hits, misses, maxsize, currsize)
    with f.cache_info().  Clear the cache and statistics with f.cache_clear().
    Access the underlying function with f.__wrapped__.

    See:  https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)
functools.namedtuple
Python Library Documentation: function namedtuple in module collections

namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)
    Returns a new subclass of tuple with named fields.

    >>> Point = namedtuple('Point', ['x', 'y'])
    >>> Point.__doc__                   # docstring for the new class
    'Point(x, y)'
    >>> p = Point(11, y=22)             # instantiate with positional args or keywords
    >>> p[0] + p[1]                     # indexable like a plain tuple
    33
    >>> x, y = p                        # unpack like a regular tuple
    >>> x, y
    (11, 22)
    >>> p.x + p.y                       # fields also accessible by name
    33
    >>> d = p._asdict()                 # convert to a dictionary
    >>> d['x']
    11
    >>> Point(**d)                      # convert from a dictionary
    Point(x=11, y=22)
    >>> p._replace(x=100)               # _replace() is like str.replace() but targets named fields
    Point(x=100, y=22)
functools.partial
Python Library Documentation: class partial in module functools

class partial(builtins.object)
 |  partial(func, *args, **keywords) - new function with partial application
 |  of the given arguments and keywords.
 |
 |  Methods defined here:
 |
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.
 |
 |  __delattr__(self, name, /)
 |      Implement delattr(self, name).
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __reduce__(...)
 |      Helper for pickle.
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __setattr__(self, name, value, /)
 |      Implement setattr(self, name, value).
 |
 |  __setstate__(...)
 |
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |
 |  __class_getitem__(...)
 |      See PEP 585
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs)
 |      Create and return a new object.  See help(type) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |
 |  __vectorcalloffset__
 |
 |  args
 |      tuple of arguments to future partial calls
 |
 |  func
 |      function object to use in future partial calls
 |
 |  keywords
 |      dictionary of keyword arguments to future partial calls
functools.partialmethod
Python Library Documentation: class partialmethod in module functools

class partialmethod(builtins.object)
 |  partialmethod(func, /, *args, **keywords)
 |
 |  Method descriptor with partial application of the given arguments
 |  and keywords.
 |
 |  Supports wrapping existing descriptors and handles non-descriptor
 |  callables as instance methods.
 |
 |  Methods defined here:
 |
 |  __get__(self, obj, cls=None)
 |
 |  __init__(self, func, /, *args, **keywords)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  __repr__(self)
 |      Return repr(self).
 |
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |
 |  __class_getitem__ = GenericAlias(...)
 |      Represent a PEP 585 generic type
 |
 |      E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).
 |
 |  ----------------------------------------------------------------------
 |  Readonly properties defined here:
 |
 |  __isabstractmethod__
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables
 |
 |  __weakref__
 |      list of weak references to the object
functools.recursive_repr
Python Library Documentation: function recursive_repr in module reprlib

recursive_repr(fillvalue='...')
    Decorator to make a repr function return fillvalue for a recursive call
functools.reduce
Python Library Documentation: built-in function reduce in module _functools

reduce(...)
    reduce(function, iterable[, initial]) -> value

    Apply a function of two arguments cumulatively to the items of a sequence
    or iterable, from left to right, so as to reduce the iterable to a single
    value.  For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
    of the iterable in the calculation, and serves as a default when the
    iterable is empty.
functools.singledispatch
Python Library Documentation: function singledispatch in module functools

singledispatch(func)
    Single-dispatch generic function decorator.

    Transforms a function into a generic function, which can have different
    behaviours depending upon the type of its first argument. The decorated
    function acts as the default implementation, and additional
    implementations can be registered using the register() attribute of the
    generic function.
functools.singledispatchmethod
Python Library Documentation: class singledispatchmethod in module functools

class singledispatchmethod(builtins.object)
 |  singledispatchmethod(func)
 |
 |  Single-dispatch generic method descriptor.
 |
 |  Supports wrapping existing descriptors and handles non-descriptor
 |  callables as instance methods.
 |
 |  Methods defined here:
 |
 |  __get__(self, obj, cls=None)
 |
 |  __init__(self, func)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  register(self, cls, method=None)
 |      generic_method.register(cls, func) -> func
 |
 |      Registers a new implementation for the given *cls* on a *generic_method*.
 |
 |  ----------------------------------------------------------------------
 |  Readonly properties defined here:
 |
 |  __isabstractmethod__
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables
 |
 |  __weakref__
 |      list of weak references to the object
functools.total_ordering
Python Library Documentation: function total_ordering in module functools

total_ordering(cls)
    Class decorator that fills in missing ordering methods
functools.update_wrapper
Python Library Documentation: function update_wrapper in module functools

update_wrapper(wrapper, wrapped, assigned=('__module__', '__name__', '__qualname__', '__doc__', '__annotations__', '__type_params__'), updated=('__dict__',))
    Update a wrapper function to look like the wrapped function

    wrapper is the function to be updated
    wrapped is the original function
    assigned is a tuple naming the attributes assigned directly
    from the wrapped function to the wrapper function (defaults to
    functools.WRAPPER_ASSIGNMENTS)
    updated is a tuple naming the attributes of the wrapper that
    are updated with the corresponding attribute from the wrapped
    function (defaults to functools.WRAPPER_UPDATES)
functools.wraps
Python Library Documentation: function wraps in module functools

wraps(wrapped, assigned=('__module__', '__name__', '__qualname__', '__doc__', '__annotations__', '__type_params__'), updated=('__dict__',))
    Decorator factory to apply update_wrapper() to a wrapper function

    Returns a decorator that invokes update_wrapper() with the decorated
    function as the wrapper argument and the arguments to wraps() as the
    remaining arguments. Default arguments are as for update_wrapper().
    This is a convenience function to simplify applying partial() to
    update_wrapper().