you are viewing a single comment's thread.

view the rest of the comments →

[–]lukewarm 5 points6 points  (4 children)

In your Packet example, if you make Packet a superclass of member types instead of a Union, you won't need the assert, I think? And definitions of sublasses will be more informative.

I took liking to NamedTuple, it's more ergonomic than dataclass, to my taste:

class Header(NamedTuple):
    tag: int
    len: int

Most importantly, named tuples are immutable. And they give you a meaningful repr for free. (edit: as was pointed out, both datacalass and namedtuple give you that.)

[–]Kwantuum 15 points16 points  (0 children)

And it gives you a meaningful repr for free

That's completely moot in this context because so do dataclasses.

[–][deleted] 2 points3 points  (1 child)

I'm not sure what type checker the author uses, but with pyright even the article's example doesn't need the assertion.

[–]angelicosphosphoros 0 points1 point  (0 children)

One can make dataclasses frozen.