In Python, I want to have an enum-like data structure that can be used like the Option
enum in Rust: the enum has parameterized member, and an unparameterized, e.g.,
enum MyType<T> {
A,
B(T),
}
would allow me to use MyType.A
, MyType.B(1)
and MyType.B(2)
.
What is the most elegant way to achieve this behavior in Python? So far, my only solution would involve subclasses A
, B
of an abstract class MyType
. But the drawback is that this requires me to do instance checking instead of simply using is
.
My specific use case is the following: My data can be either replicated across multiple processes or sharded. If it’s sharded, I need the number of shards. My function will process data depending on whether it’s replicated or sharded.
If enums aren’t useful for this, how to better implement this?
3
Python enums are meant to have a fixed membership. Your example shows an unlimited number of MyType.B(x)
members. This is not a good fit.
2