"Steven Bethard" steven.bethard@GMAIL.COM wrote: [snip]
In short, for object construction and attribute access, the Record class is faster (because __init__ is a simple list of assignments and attribute access is through C-level slots). For unpacking and iteration, the NamedTuple factory is faster (because it's using the C-level tuple iteration instead of a Python-level generator).
Somewhere in the back of my mind something is telling me - if you can get a tuple with attributes based on __slots__, you just duplicate the references as both an attribute AND in the standard tuple PyObject* array, which could offer good speed in both cases, at the cost of twice as many pointers as the other two options.
But alas, it isn't possible right now...
>>> class foo(tuple): ... __slots__ = 'a' ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Error when calling the metaclass bases nonempty __slots__ not supported for subtype of 'tuple'
Ah well. Anyone have any intuition as to what they will be doing with named tuples/record objects enough to know which case is better?
Also, it doesn't seem like it would be terribly difficult to use metaclasses to get the ease of creation from the Record type for the NamedTuple variant, if desired.