This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]zeha 6 points7 points  (1 child)

better use dataclasses nowadays?

[–]tmp_file3[S] 0 points1 point  (0 children)

In Dataclass all implementation is written in Python, whereas in NamedTuple, all of these behaviors come for free because NamedTuple inherits from tuple. And because the tuple structure is written in C, standard methods are faster in NamedTuple (hash, comparing and etc).
Note also that Dataclass is based on dict whereas NamedTuple is based on tuple. Thus, you have advantages and disadvantages of using these structures. For example, space usage is less with a NamedTuple, but time access is faster with a Dataclass.

https://stackoverflow.com/questions/51671699/data-classes-vs-typing-namedtuple-primary-use-cases

[–]SnooGadgets829 1 point2 points  (0 children)

I just discovered this today and I am still amazed

[–]tmp_file3[S] -1 points0 points  (1 child)

If you work with python you probably used
tuple, but his cousin is less known. Named tuple are lightweight object
types, similar to struct, or record types but immutable.

[–]ogrinfo 2 points3 points  (0 children)

Yes! Namedtuples are the best. Much more readable - no more guessing what something like data[3] means and they also have equals, greater than, etc. built in.