Loading video player…

Getting to Know Enumerations

00:00 Getting to know enumerations in Python.

00:04 Several programming languages such as Java and C++ have a native enumeration or enum data type as part of their syntax. This allows you to create sets of named constants, which are considered members of the containing enum.

00:17 You can access the members through the enumeration itself. Enumerations come in handy when you need to define an immutable and discrete set of similar or related constant values that may or may not have semantic meaning in your code. Days of the week, months, seasons of the year, Earth’s cardinal directions, program status codes, HTTP status codes, colors in a traffic light, and pricing plans of a web service are all great examples of enumerations in programming.

00:47 In general, you can use an enum whenever you have a variable that can take one of a limited set of possible values.

00:55 Python doesn’t have an enum data type as part of its syntax. Fortunately, Python 3.4 added the enum module to the standard library.

01:05 This module provides the Enum class for supporting general-purpose enumerations in Python. They were introduced by PEP 435, which defines them as seen on screen before.

01:18 This addition to the standard library, you could create something similar to an enumeration by defining a sequence of similar or related constants. Python developers often use the idiom seen on screen.

01:38 Even though this works, it doesn’t scale well when you are trying to group a large number of related constants. Another inconvenience is that the first constant will have a value of zero, which is falsy in Python.

01:50 This can be an issue in some situations, especially those involving Boolean tests.

01:58 If you’re using a Python version prior to 3.4, then you can create enumerations by installing the enum34 library, which is a backport of the standard library enum. The aenum third-party library could be an option for you as well.

02:14 Enumerations have several benefits, some of which relate to ease of coding, allowing for conveniently grouped related constants in a kind of namespace, allowing for additional behavior with custom methods that operate on either enum members or the enum itself.

02:30 Providing quick and flexible access to enum members,

02:34 enabling direct iteration over members, including their names and values, facilitating code completion within IDEs and editors, enabling type and error checking with static checkers, providing a hub of searchable names and mitigating spelling mistakes when using the members of an enumeration.

02:55 They also provide a number of benefits, ensuring constant values that can’t be changed during the code’s execution, guaranteeing type safety by differentiating the same value shared across several enums, improving readability and maintainability by using descriptive names instead of mysterious values or magic numbers.

03:15 Facilitating debugging by taking advantage of readable names instead of values with no explicit meaning and providing a single source of truth and consistency throughout the code.

03:29 Now that you know the basics of enumerations in programming and in Python, you can start creating your own enum types by using Python’s Enum class, and that’s what you’ll be doing in the next part of the course.

Become a Member to join the conversation.