4 ms·
In my experience, namedtuples have significantly less overhead than classes.
by CardenB 10y ago
In my experience, namedtuples have significantly less overhead than classes.
- kchoudhu 10y agoYes for the increased overhead, but I find that the fact that they're tuples underneath results in odd semantics (I think the comment above mine[1] does a decent job pointing out why I am leary of using them. Tradeoffs, I guess. [1] https://news.ycombinator.com/item?id=13124501 https://news.ycombinator.com/item?id=13124501
- skrause 10y agoI doubt that because defining a namedtuple just creates a new class internally: https://github.com/python/cpython/blob/master/Lib/collections/__init__.py#L304 https://github.com/python/cpython/blob/master/Lib/collection... Class creation the very expensive in Python, so Guido van Rossum explicitly recommends against using dynamically created namedtuples: https://mail.python.org/pipermail//python-ideas/2016-April/039867.html https://mail.python.org/pipermail//python-ideas/2016-April/0...
- tcbawo 10y agoI believe that objects of namedtuple have less runtime overhead than a class. Guido seems to point out that the process of instantiating a namedtuple class is expensive.
- benhoyt 10y agoNormally, yes. But if you define __slots__ on the class, it's the same overhead as a namedtuple (no __dict__ per instance).