Feb-12-2022, 05:01 PM
(This post was last modified: Feb-12-2022, 05:01 PM by deanhystad.)
From the docs you referenced:
Output:When to use __new__() vs. __init__()
__new__() must be used whenever you want to customize the actual value of the Enum member. Any other modifications may go in either __new__() or __init__(), with __init__() being preferred.
In other words use __new__ when you want to an enum that has a different type of value. Their example used bytes. Your value is a str.class MyEnum(str, Enum): def __new__(cls, value, extra): obj = str.__new__(cls, [value]) obj._value_ = value obj.extra = extra return obj MEMBER = ("This is my enum", 1.5) print(MyEnum.MEMBER) print(MyEnum.MEMBER.value) print(MyEnum.MEMBER.extra)
Output:MyEnum.MEMBER
This is my enum
1.5
You were trying to create a new enum whose value was your new enum class.