Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python functools.namedtuple

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?

like image 750
lvc Avatar asked May 23 '12 15:05

lvc


People also ask

What does Functools do in Python?

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.

What does Functools wrap do?

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.

Is Functools a standard Python library?

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

What is Functools reduce in Python?

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.


1 Answers

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.

like image 176
NPE Avatar answered Oct 14 '22 06:10

NPE