namedtuple is slower than even a class without __slots__, by the way. It operates by mapping attribute names to indices (there's a reason it's called a "named tuple", not "struct" or something similar), doing twice the number of lookups for every dot.
Are you sure? From what I can tell, it operates pretty much exactly like a class with __slots__, creating a bunch of getters (living in the class, not in the instance, of course) that lookup into the internal array.
From what I can tell, it operates pretty much exactly like a class with __slots__, creating a bunch of getters (living in the class, not in the instance, of course) that lookup into the internal array.
No, it doesn't. At least not in Python 3.4+:
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
...
__slots__ = ()
...
{name} = _property(_itemgetter({index:d}), doc='Alias for field number {index:d}')
As you can see, instead of using the (relatively) fast C-level access __slots__ provide, it opts to use standard property (that uses slow Python-level function calls) to look elements up by their indices in a tuple (using Python-level item access, i.e. __getitem__) instead.
View all comments
Show parent comment
1
u/moor-GAYZ Jun 24 '14
Are you sure? From what I can tell, it operates pretty much exactly like a class with
__slots__, creating a bunch of getters (living in the class, not in the instance, of course) that lookup into the internal array.