IntEnum and Flag
Use integer and flag enums.
IntEnum and Flag is a free Python Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond Plain Enum
The enum module ships specialized base classes. IntEnum members behave like integers, and Flag members can be combined with bitwise operators. They cover cases where plain Enum is too restrictive.
Plain Enum Is Not an int
A plain Enum member is not interchangeable with its integer value. Comparing it to a number returns False.
from enum import Enum
class Level(Enum):
LOW = 1
HIGH = 2
print(Level.LOW == 1)IntEnum Compares as int
An IntEnum member is an integer, so it compares equal to its numeric value and supports ordering.
from enum import IntEnum
class Level(IntEnum):
LOW = 1
HIGH = 2
print(Level.LOW == 1)
print(Level.LOW < Level.HIGH)IntEnum in Arithmetic
Because members are real integers, you can use them in math and indexing. Use this when a value really needs to interoperate with numeric code.
from enum import IntEnum
class Priority(IntEnum):
LOW = 1
MEDIUM = 5
HIGH = 10
print(Priority.HIGH + 1)
print(Priority.MEDIUM * 2)Sorting IntEnum Members
Since IntEnum supports ordering, a list of members sorts by value with no extra key function.
from enum import IntEnum
class Priority(IntEnum):
LOW = 1
MEDIUM = 5
HIGH = 10
for p in sorted([Priority.HIGH, Priority.LOW, Priority.MEDIUM]):
print(p.name)Flag for Combinable Options
A Flag enum is meant for values you combine. Assign powers of two so each member occupies its own bit.
from enum import Flag, auto
class Permission(Flag):
READ = auto()
WRITE = auto()
EXECUTE = auto()
print(Permission.READ)
print(Permission.READ.value)Combining Flags with |
Use the bitwise OR operator | to combine flags into a single composite value.
from enum import Flag, auto
class Permission(Flag):
READ = auto()
WRITE = auto()
EXECUTE = auto()
access = Permission.READ | Permission.WRITE
print(access)Testing Membership with in
Check whether a flag is set using the in operator, which performs the bitwise test for you.
from enum import Flag, auto
class Permission(Flag):
READ = auto()
WRITE = auto()
EXECUTE = auto()
access = Permission.READ | Permission.WRITE
print(Permission.READ in access)
print(Permission.EXECUTE in access)Intersection and Removal
Combine flags with & (AND) to find common bits, and remove a flag with & against the complement ~.
from enum import Flag, auto
class Permission(Flag):
READ = auto()
WRITE = auto()
EXECUTE = auto()
access = Permission.READ | Permission.WRITE
still = access & ~Permission.WRITE
print(still)IntFlag for Integer Flags
IntFlag combines both ideas: combinable flags that also behave like plain integers, useful when interfacing with C APIs or bit masks stored as numbers.
from enum import IntFlag, auto
class Mode(IntFlag):
R = 4
W = 2
X = 1
m = Mode.R | Mode.W
print(int(m))Iterating a Combined Flag
Looping over a combined Flag value yields each individual flag it contains, which is handy for listing active options.
from enum import Flag, auto
class Permission(Flag):
READ = auto()
WRITE = auto()
EXECUTE = auto()
access = Permission.READ | Permission.EXECUTE
for p in access:
print(p.name)Quick Check
Why would you choose a Flag enum over a plain Enum?
Recap
You learned integer and flag enums.
IntEnummembers behave like integers and support ordering and arithmetic.Flagmembers combine with|,&,~and test within.IntFlagmixes both behaviors.- Use powers of two for flag values.
Frequently asked questions
Is the “IntEnum and Flag” lesson free?
Yes — the full text of “IntEnum and Flag” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “IntEnum and Flag”?
Use integer and flag enums. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “IntEnum and Flag” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Enum Class
- IntEnum and Flag
- NamedTuple Basics
- typing.NamedTuple