Reputation: 348
I would like to create my own enum. This will have names but no values. When calling this enum it should always return the name.
from enum import Enum
class myEnum(Enum):
def __repr__(self):
return self.name
my_enum = myEnum('enum', ['a', 'b'])
With:
print(my_enum.a)
it will returns a. That's ok.
But using this in a class:
class T():
def do_something(self):
print(my_enum.a)
With:
T().do_something()
will return enum.a
Goal is this will always return a.
Upvotes: 14
Views: 9953
Reputation: 1575
If When calling this enum it should always return the name means when a string
is required then you can add this method to the myEnum
class:
def __str__(self):
return self.name
Upvotes: 27