I am aware of the existence and purpose of collections.namedtuple
, but I have noticed that, at least in IDLE (3.2.2), this factory function is also in functools
:
>>> import functools
>>> functools.namedtuple
<function namedtuple at 0x024B41E0>
It also exists in collections
as expected, and is the same function:
>>> import collections
>>> collections.namedtuple is functools.namedtuple
True
No docs I can find ever mention namedtuple
being anywhere other than collections. So: is this standard, or just an IDLE weirdness? If it's just IDLE, is it a bug or a Why would namedtuple
be in two places - and, indeed, in whose warped mind does it make sense in functools
of all places?
Functools module is for higher-order functions that work on other functions. It provides functions for working with other functions and callable objects to use or extend them without completely rewriting them. This module has two classes – partial and partialmethod.
functools. wraps is 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). So @wraps decorator actually gives a call to functools.
The functools module, part of Python's standard Library, provides useful features that make it easier to work with high order functions (a function that returns a function or takes another function as an argument ).
reduce() in Python The reduce(fun,seq) function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along. This function is defined in “functools” module. Working : At first step, first two elements of sequence are picked and the result is obtained.
In Python 3.2.2, functools.py
contains the following import:
from collections import OrderedDict, namedtuple
It seems pretty clear that it's just a convenience import for the module's implementation, and is not intended to be part of its public interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With