[Python-ideas] Add recordlcass to collections module
Martin Bammer
mrbm74 at gmail.com
Sun Sep 2 16:56:50 EDT 2018
Hi,
then intention of my first mail was to start a discussion about this
topic about the pros and cons and possible alternatives.
As long as it is not clear that recordclass or something like that is
accepted to be implemented to the collections module
I do not want to spend any effort on this.
My wish that the collections module gets something like namedtuple, but
writable, is based on my personal experience when projects are becoming
bigger and data structures more complex it is sometimes useful to named
items and not just an index. This improves the readability and makes
development and maintenance of the code easier.
Another important topic for me is performance. When I write applications
then they should finish their tasks quickly. The performance of
recordclass was one reason for me to use it (some benchmarks with Python
2 can be found on here
https://gist.github.com/grantjenks/a06da0db18826be1176c31c95a6ee572).
I've done some more recent and additional benchmarks with Python 3.7 on
Linux which you can find here https://github.com/brmmm3/compare-recordclass.
These new benchmarks show that namedtuple is as fast as recordclass in
all cases, but with named attribute access. Named attribute access is
faster with recordclass.
Compared to dataclass:
dataclass wins only on the topic object size. When it comes to speed and
functionality (indexing, sorting) dataclass would be my last choice.
Yes it is possible to make dataclass fast by using __slots__, but this
always an extra programming effort. namedtuple and recordclass are easy
to use with small effort.
Adding new items:
This is not possible with namedtuple and also not possible with
recordclass. I see no reason why a namedlist should support this,
because with these object types you define new object types and these
types should not change.
I hope 3.8 will get a namedlist and maybe it will be the recordclass
module (currently my choice). As the author of this module already has
responded to this discussion I hope he willing to contribute his code to
the Python project.
Best regards,
Martin
More information about the Python-ideas
mailing list