Using Enumerations in if and match Statements
Using enumerations in
match statements. Chained
elif statements and the
case statement introduced in Python 3.10 are common and arguably natural places where you can use enumerations.
00:15 Both constructs allow you to take different courses of action depending on certain conditions. Let’s say you have a piece of code that handles a semaphore or a traffic light in a traffic control application.
the chain of statements in. The
handle_semaphore() function checks the value of the current light to decide on the action to take. Note that the calls to
handle_semaphore() are just placeholders. In real code, you’d replace them with more complex operations.
Using either technique is a matter of taste and style. Both work well and are comparable in terms of readability. But note, if you need to guarantee backward compatibility with Python versions lower than 3.10, then you must use chained
Become a Member to join the conversation.