Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the namedtuple module use a metaclass to create nt class objects?

Tags:

I spent some time investigating the collections.namedtuple module a few weeks ago. The module uses a factory function which populates the dynamic data (the name of the new namedtuple class, and the class attribute names) into a very large string. Then exec is executed with the string (which represents the code) as the argument, and the new class is returned.

Does anyone know why it was done this way, when there is a specific tool for this kind of thing readily available, i.e. the metaclass? I haven't tried to do it myself, but it seems like everything that is happening in the namedtuple module could have been easily accomplished using a namedtuple metaclass, like so:

class namedtuple(type): 

etc etc.

like image 565
Rick supports Monica Avatar asked Jan 28 '15 04:01

Rick supports Monica


People also ask

What is Namedtuple used for?

Python's namedtuple() is a factory function available in collections . It allows you to create tuple subclasses with named fields. You can access the values in a given named tuple using the dot notation and the field names, like in obj.

What is Typename in Namedtuple?

namedtuple() is a factory function for tuple subclasses. Here, 'whatsmypurpose' is the type name. When you create a named tuple, a class with this name ( whatsmypurpose ) gets created internally. You can notice this by using the verbose argument like: Point=namedtuple('whatsmypurpose',['x','y'], verbose=True)

Is Namedtuple fast?

NamedTuple is the faster one while creating data objects (2.01 µs). An object is slower than DataClass but faster than NamedTuple while creating data objects (2.34 µs).


2 Answers

There are some hints in the issue 3974. The author proposed a new way to create named tuples, which was rejected with the following comments:

It seems the benefit of the original version is that it's faster, thanks to hardcoding critical methods. - Antoine Pitrou

There is nothing unholy about using exec. Earlier versions used other approaches and they proved unnecessarily complex and had unexpected problems. It is a key feature for named tuples that they are exactly equivalent to a hand-written class. - Raymond Hettinger

Additionally, here is the part of the description of the original namedtuple recipe:

... the recipe has evolved to its current exec-style where we get all of Python's high-speed builtin argument checking for free. The new style of building and exec-ing a template made both the __new__ and __repr__ functions faster and cleaner than in previous versions of this recipe.

If you're looking for some alternative implementations:

  • abstract base class + mix-in for named tuples recipe by Jan Kaliszewski

  • metaclass-based implementation by Aaron Iles (see his blog post)

like image 162
vaultah Avatar answered Sep 21 '22 21:09

vaultah


As a sidenote: The other objection I see most often against using exec is that some locations (read companies) disable it for security reasons.

Besides an advanced Enum and NamedConstant, the aenum library* also has NamedTuple which is metaclass-based.


* aenum is written by the author of enum and the enum34 backport.

like image 36
Ethan Furman Avatar answered Sep 22 '22 21:09

Ethan Furman