[Python] Parameterized Enums in Python

Discussão em 'Python' iniciado por Stack, Setembro 24, 2024.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página