[Python-ideas] namedtuple with ordereddict

Steven D'Aprano steve at pearwood.info
Tue Jul 18 21:27:02 EDT 2017

On Tue, Jul 18, 2017 at 06:16:26PM -0400, Jim J. Jewett wrote:
> Given that
> (1) dicts now always pay the price for ordering
> (2) namedtuple is being accelerated
> is there any reason not to simply define it as a view on a dict, or at
> least as a limited proxy to one?

Tuples are much more memory efficient than dicts, they support lookup by 
index, and you'll break a whole lot of code that treats namedtuples as 
tuples and performs tuple operations on them. For instance, tuple 

> Then constructing a specific instance from the arguments used to
> create it could be as simple as keeping a reference to the temporary
> created to pass those arguments...

The bottleneck isn't creating the instances themselves, the expensive 
part is calling namedtuple() to generate the named tuple CLASS itself. 
Creating the instances themselves should be fast, they're just tuples.


More information about the Python-ideas mailing list