[Python-ideas] A subclassing API for named tuples?
Antoine Pitrou
solipsis at pitrou.net
Thu Feb 14 15:56:55 CET 2013
Le Thu, 14 Feb 2013 23:19:51 +1000,
Nick Coghlan <ncoghlan at gmail.com> a
écrit :
> An exchange with Antoine in one of the enum threads sparked a thought.
>
> A recurring suggestion for collections.namedtuple is that it would be
> nice to be able to define them like this (as it not only avoids having
> to repeat the class name, but also allows them to play nicely with
> pickle and other name-based reference mechanisms):
>
> class MyTuple(collections.NamedTuple):
> __fields__ = "a b c d e".split()
>
> However, one of Raymond's long standing objections to such a design
> for namedtuple is the ugliness of people having to remember to include
> the right __slots__ definition to ensure it doesn't add any storage
> overhead above and beyond that for the underlying tuple.
You don't *have* to remember to include it. You just have to include it
if you really care about the memory footprint. Which is something that
is easy to mention in the documentation.
(also, an empty dict that never gets accessed is not much of a
problem performance-wise)
Speaking for myself, most uses of namedtuple are not
performance-critical. They are for convenience: I want an immutable,
hashable, comparable record-like class (with a useful definition of
equality, which is also very convenient for unit tests :-)) and I don't
want to write that behaviour by hand every time.
Regards
Antoine.
More information about the Python-ideas
mailing list