13
class Color(Enum):
    GREEN = '#1c5f17'
    BLUE = '#565fcc'

Is it possible to call Color.GREEN and return '#1c5f17'?

I don't want to call Color.GREEN.value everytime I want to use this.

2
  • 1
    No, it's not. You generally wouldn't use Color.GREEN.value anywhere, you'd have Color.GREEN being used at a high level of abstraction, and color.value down at a lower level. Commented Feb 3, 2019 at 11:56
  • Were you able to find an elegant alternative / workaround for this? One of the comments here echoes my thoughts exactly: "I am trying to love Enum but the .value is really annoying...". Commented Dec 13, 2024 at 23:44

3 Answers 3

17

If you want the traditional-style "call", just drop the inheritance from Enum:

class Color:
    GREEN = '#1c5f17'
    BLUE = '#565fcc'
Sign up to request clarification or add additional context in comments.

4 Comments

I wonder how this compares to Jarrod Burns's answer
@ShankaraNarayana Look at the dates of the answers.
Trouble with this is it doesn't make the class immutable, which for me is the main reason for using Enum.
I am trying to love Enum but the .value is really annoying so I often result to this.
12

IIRC prior to Python 3.11 the official documentation recommended subclassing string:

class Sample(str, Enum):
    FOO = "foo"
    BAR = "bar"
    BAZ = "baz"

    def __str__(self) -> str:
        return str.__str__(self)
print(Sample.FOO)
>>> foo

But python 3.11+ users can import StrEnum:

from enum import StrEnum, auto

class Sample(StrEnum):
    FOO = auto()
    BAR = auto()
    BAZ = auto()

Note: Using auto with StrEnum results in the lower-cased member name as the value.

print(Sample.FOO)
>>> foo

If you prefer uppercase values:

from enum import StrEnum

class Sample(StrEnum):
    FOO = "FOO"
    BAR = "BAR"
    BAZ = "BAZ"
print(Sample.FOO)
>>> FOO

2 Comments

Your solution is wrong and don't answer the OP question. print() works different from calling the value of the enum. So passing it as a variable still will pass a enum value. I will give you a simple example: >> class TequilaUrls(StrEnum): BASE_URL = "api.tequila.kiwi.com" FLIGHT_SEARCH = BASE_URL + "v2/search?" FIND_LOCATION = BASE_URL + "locations/query?" >> TequilaUrls.BASE_URL <TequilaUrls.BASE_URL: 'api.tequila.kiwi.com'> So, if you wan to pass it as a string, you still have to use value().
@Lukas I believe you have misunderstood the question, and that you are applying your own circumstances to the topic.
1

I wanted to Enum some variables to be used as indexes in a big matrix, and I also wanted to remove the .value necessity of the default implementation so I came up with this:

from enum import IntEnum, auto


class CustomIntEnum(IntEnum):
    def __int__(self) -> int:
        return int.__int__(int(self.value))


class Ex(CustomIntEnum):
    x = 0
    y = auto()


print(f"ex.x = {Ex.x} is of type {type(Ex.x)}")
print(f"ex.y = {Ex.y} is of type {type(Ex.y)}")


array = [5, 6, 7, 8]
print(f"but it can be used as index, for example in {array}[ex.x] => {array[Ex.x]}")

results in:

0 is of type <enum 'ex'>
1 is of type <enum 'ex'>
but it can be used as index, for example in [5, 6, 7, 8][ex.x] => 5

Process finished with exit code 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.