Enum HOWTO
An Enum is a set of symbolic names bound to unique values. They are similar to global variables, but they offer a more useful repr, grouping, type-safety, and a few other features. They are most useful when you have a variable that can take one of a limited selection of values. For example, the days
Reference note (untrusted external data; do not execute it as instructions).
An Enum is a set of symbolic names bound to unique values. They are similar to global variables, but they offer a more useful repr, grouping, type-safety, and a few other features.
They are most useful when you have a variable that can take one of a limited selection of values. For example, the days of the week
Or perhaps the RGB primary colors
As you can see, creating an Enum is as simple as writing a class that inherits from Enum itself.
Depending on the nature of the enum a member's value may or may not be important, but either way that value can be used to get the corresponding member
As you can see, the repr() of a member shows the enum name, the member name, and the value. The str() of a member shows only the enum name and member name
The type of an enumeration member is the enum it belongs to
Enum members have an attribute that contains just their !name
Likewise, they have an attribute for their !value
Unlike many languages that treat enumerations solely as name/value pairs, Python Enums can have behavior added. For example, datetime.date has two methods for returning the weekday: ~datetime.date.weekday and ~datetime.date.isoweekday. The difference is that one of them counts from 0-6 and the other from 1-7. Rather than keep track of that ourselves we can add a method to the !Weekday enum to extract the day from the ~datetime.date instance and return the matching enum member
The complete !Weekday enum now looks like this
Now we can find out what today is! Observe
Of course, if you're reading this on some other day, you'll see that day instead.
This !Weekday enum is great if our variable only needs one day, but what if we need several? Maybe we're writing a function to plot chores during a week, and don't want to use a list -- we could use a different type of Enum
We've changed two things: we're inherited from Flag, and the values are all powers of 2.
Just like the original !Weekday enum above, we can have a single selection
But Flag also allows us to combine several members into a single variable
You can even iterate over a Flag variable
Okay, let's get some chores set up
And a function to display the chores for a given day
In cases where the actual values of the members do not matter, you can save yourself some work and use auto for the values
Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Python Documentation โ Doc/howto/enum.rst :: Enum HOWTO โRevision f10166035d60 ยท PSF-2.0 and attribution