News for hackers. Stuff that matters.
Re:Using enums to represent state in Rust by the__alchemist on Friday September 22, 2023 @09:18AM (#37611649)
I joke to myself that I program with "struct and enum-oriented programming". I got it from Rust, but apply it to Python too. (Python enums aren't as ergonomic, and they can't wrap values, but they're a start)
Re:Using enums to represent state in Rust by qsort on Friday September 22, 2023 @09:50AM (#37612047)
Python's equivalent of Rust's Enums would be the __match_args__ machinery rather than enums themselves, ironically.

They aren't as ergonomic or type-safe, and rather surprisingly the match statement is not an expression in Python's grammar, but regardless of its problems the match statement is very powerful, even more so than static equivalents.

Re:Using enums to represent state in Rust by GalaxySnail on Friday September 22, 2023 @01:15PM (#37614713)
IMO Python's equivalent of Rust's Enums would be `typing.Union` [1]. It is more ergonomic and type-safe, and mypy supports type narrowing [2] and exhaustiveness checking on it [3].

[1] https://docs.python.org/3/library/stdtypes.html#types-union

[2] https://mypy.readthedocs.io/en/stable/type_narrowing.html

[3] https://docs.python.org/3/library/typing.html#typing.assert_...