5

The aim is to use an Enum class to allow for predictable input of a definite amount of options.

I expected to get the integers as output, however I get class.attrib if I print it.

How can I get the integer instead?

from enum import Enum

class Stiffness(Enum):
    FREE = 0
    RIGID = 1
    FLEXIBLE = 2

print(Stiffness.FLEXIBLE)

Output: Stiffness.FLEXIBLE

Expected output: 2

2 Answers 2

8

Just print the value:

print(Stiffness.FLEXIBLE.value)
# 2

You can have a look at the part of the enum documentation about attributes of enum members.

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

Comments

4

You can use IntEnum instead of Enum.

This allows to use Stiffness.FLEXIBLE interchangeably with the integer 2, for example you can pass it to a function that expects an integer.

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.