3 ms·
We don’t use Python native enums
- brentsch 6y agoA key line seems to be buried in the "Summary" at the end of the post: "Python native enums are great for what they were designed to do, but..." Barring tricks I'm unaware of, enums in other popular languages, such as Go and TypeScript, don't support the author's more complex usage either—probably by design. Out of curiosity, are there languages for which the enum implementation supports and encourages encoding complex data like this?
- nine_k 6y agoMaybe Java? https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html https://docs.oracle.com/javase/tutorial/java/javaOO/enum.htm... Look how they encode planetary data into the enum of the Solar system planets.
- isaac21259 6y agoI think rust encourages encoding data like that. https://doc.rust-lang.org/rust-by-example/custom_types/enum.html https://doc.rust-lang.org/rust-by-example/custom_types/enum....
- rmdashrfstar 6y agoSum types are the main reason I love writing Rust code. We all miss sum type Enums wherever we go. [0] [0] https://www.reddit.com/r/rust/comments/l594zl/everywhere_i_go_i_miss_rusts_enums/?utm_source=share&utm_medium=ios_app&utm_name=iossmf https://www.reddit.com/r/rust/comments/l594zl/everywhere_i_g...
- remram 6y agoYes, you can search for "tagged union"
- habitue 6y agoOften called sum types, it's really convenient when your language supports both building these kinds of enums as well as destructuring them with pattern matching. If you don't have destructuring, they're less attractive. There's a proposal to add destructuring to python[1] so we'll see [1] https://www.python.org/dev/peps/pep-0634/ https://www.python.org/dev/peps/pep-0634/
- jasonpeacock 6y agoYeah...those are all not the use cases that Enums were intended/designed for. Juse use Python's `dataclass` or similar (`pydantic` is great) and just build the custom data types that you need.
- stevenjohns 6y agoPart of the reason why Enums weren’t originally added to the stdlib was because if you get ten developers in a room there will be eleven conflicting expectations from an Enum class. Personally I’ve never needed to use the Enum class, ever, in the last five years of Python development. It takes barely any time to implement a custom replacement if you need one, but for 99% of my use cases a dictionary or tuple has been enough.
- zaphirplane 6y agoThese lite Enums are syntactic sugar over a bunch of constant strings and those strings inserted in a list So yes you can always use something else just like all the fold/zip/map can be replaced with also something else ;)
- mixmastamyk 6y agoEnums are implemented in a weird way in Python, not as part of the language but as a class that modifies itself. That said, this article is not what I expected.
- comfydragon 6y agoThey're implemented using metaclasses. Metaprogramming is a part of the language.
- mixmastamyk 6y agoI know that, there's a lot of magic going on due to that choice of indirect implementation.
- twelve40 6y agoA very odd construct. Coming from Java, a big disappointment, Python enums, pretty much useless. To those who say "just roll your own", sure, but why not make the existing one actually usable?