How to get new Enum with members as enum using EnumMeta Python 3 6

preview_player
Показать описание
Creating a new Enum with members as Enums using EnumMeta in Python 3.6 involves dynamic class creation and metaprogramming. The EnumMeta class is responsible for generating the actual enumeration classes, and we can leverage it to create an Enum with members as Enums. Below is a step-by-step tutorial with code examples.
This will serve as the base class for our new Enum.
This function takes the name of the new Enum (enum_name) and a dictionary of members (members). Each member is a key-value pair where the key is the member name, and the value is a tuple of values for that member.
We define a dictionary (members) where each key is the member name, and the corresponding value is a tuple containing the values for that member.
The create_enum_with_members function uses the EnumMeta class to dynamically create a new Enum class (new_enum_class). It inherits from the BaseEnum class.
Inside the function, we iterate through the members dictionary and dynamically create Enum members using new_enum_class(member_name, member_values).
Finally, we use setattr to set these members as attributes of the new Enum class.
The example usage demonstrates how to create a new Enum class and access its members.
Note: This approach is specific to Python 3.6 and may not work in earlier or later versions due to changes in the language.
ChatGPT
Рекомендации по теме
welcome to shbcf.ru