0

From Color.RED, I would like to access FOO's value of 2. Is that possible?

class Color(Enum):
    RED = BOO.FOO
    BLUE = BOO.BAR

class Person(Enum):
    FOO = 2
    BAR = 3

3 Answers 3

1

Assuming that you wanted to use Person.FOO instead of BOO.FOO:

class Person(Enum):
    FOO = 2
    BAR = 3

class Color(Enum):
    RED = Person.FOO.value
    BLUE = Person.BAR.value

Note that just using Person.FOO will get the enumeration member and not its value. If you need the value, you will have to declare it as shown above.

If you just define RED = Person.FOO, to access the value you will have to use Color.RED.value.value.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to switch the class declaration else code raised an exception: NameError: name 'Person' is not defined

class Person(Enum):
    FOO = 2
    BAR = 3

class Color(Enum):
    RED = Person.FOO.value
    BLUE = Person.BAR.value

Comments

0

BOO and Person don't match, but assuming you're wanting them to: you can access the raw value of an enum using .value and they chain together just fine.

from enum import Enum

class Boo(Enum):
    FOO = 2
    BAR = 3

class Color(Enum):
    RED = Boo.FOO
    BLUE = Boo.BAR

print(Color.RED.value.value) # prints 2

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.