I wanted to overload the getitem Enum method in Python to return the first element of a tuple instead of the whole value content. For that, I tried to create an Enum based in the EnumMeta with the redefined method, just like below, and then the Enum final class inheriting from it, just like it's shown below:
from enum import Enum
from enum import EnumMeta
from datetime import date
class CommandMeta (EnumMeta):
SUCCESS = 0, "Exited successsssfully."
def __getitem__(cls, value, *args, **kwargs):
value = value[0]
return super().__getitem__(value, *args, **kwargs)
class Command (Enum, metaclass=CommandMeta):
SUCCESS = 0, "Exited successsssfully."
The expected output was:
print(Command.SUCCESS.value)
0
And instead, it is still the same:
print(Command.SUCCESS.value)
(0, 'Exited successsssfully.')
How could I manage to overload the method properly?