Skip to content
Advertisement

Is there a way to specify a default value for python enums?

Given the following enum:

JavaScript

How can I specify a default value. I want to be able to do:

JavaScript

and havemy_val be <MyEnum.A: 0>

Is this possible? I’ve tried customizing __new__, __init__, __call__ but I can’t get it to work.

Advertisement

Answer

MyEnum(..) is handled by EnumMeta.__call__. You need to override that method:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement