News for hackers. Stuff that matters.
Re:Using Enumerated Types in Python by danthemanvsqz on Saturday June 06, 2020 @07:26PM (#23443259)
Prefer using named tuples over enums in Python.
Re:Using Enumerated Types in Python by wk_end on Saturday June 06, 2020 @07:28PM (#23443276)
Named tuples and enums do entirely different things (loosely-speaking: the former are product types, the latter are sum types).

You also probably want dataclasses [1] instead of named tuples.

[1] https://docs.python.org/3/library/dataclasses.html

Re:Using Enumerated Types in Python by zbentley on Saturday June 06, 2020 @09:59PM (#23444113)
Or data bearing enums, which bring some of the best of all of those worlds, with (value declaration, not usage) exhaustiveness enforced at compile time:

https://github.com/klaviyo/matrix_enum

My colleague and I started making use of that pattern last year (before open sourcing the package) and found it very useful.

Re:Using Enumerated Types in Python by danthemanvsqz on Saturday June 06, 2020 @11:02PM (#23444429)
They do different things but in the wild are used in the same way. Data classes are not immutable so it would not be ideal for what I used named tuples for.
Re:Using Enumerated Types in Python by wk_end on Sunday June 07, 2020 @12:16PM (#23448399)
Data classes can be made immutable using ‘frozen=True’. It sucks that it’s not default though.

Can you give an example of how you use enums and named tuples in the same way? That just doesn’t seem to make a lot of sense to me, but maybe I’m missing something.