|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/create-python-enum/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/lessons/create-python-enum/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Flessons%2Fcreate-python-enum%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-enum |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-enum |
| https://realpython.com/courses/python-enum/#team |
| Building Enumerations With Python's enum | https://realpython.com/courses/python-enum/ |
| Darren Jones | https://realpython.com/courses/python-enum/#team |
| Recommended Tutorial | https://realpython.com/python-enum/ |
| Course Slides (.pdf) | https://realpython.com/courses/python-enum/downloads/python-enum-slides/ |
| Sample Code (.zip) | https://realpython.com/courses/python-enum/downloads/python-enum-code/ |
| Ask a Question | https://realpython.com/lessons/create-python-enum/#discussion |
| https://realpython.com/feedback/survey/course/python-enum/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/python-enum/disliked/?from=lesson-title |
| Transcript | https://realpython.com/lessons/create-python-enum/#transcript |
| Discussion | https://realpython.com/lessons/create-python-enum/#discussion |
| 00:00 | https://realpython.com/lessons/create-python-enum/#t=0.875 |
| Creating enumerations with Python’s enum | https://realpython.com/lessons/create-python-enum/#t=0.875 |
| Python’s Enum module provides the Enum class, | https://realpython.com/lessons/create-python-enum/#t=4.655 |
| which allows you to create enumeration types. | https://realpython.com/lessons/create-python-enum/#t=7.345 |
| 00:10 | https://realpython.com/lessons/create-python-enum/#t=10.435 |
| To create your own enumerations, | https://realpython.com/lessons/create-python-enum/#t=10.435 |
| you can either subclass Enum, or use its functional API. | https://realpython.com/lessons/create-python-enum/#t=12.275 |
| Both options will let you define a set of related constants | https://realpython.com/lessons/create-python-enum/#t=16.205 |
| as Enum members. | https://realpython.com/lessons/create-python-enum/#t=19.135 |
| 00:22 | https://realpython.com/lessons/create-python-enum/#t=22.615 |
| The Enum module defines a general purpose enumeration type | https://realpython.com/lessons/create-python-enum/#t=22.615 |
| with iteration and comparison capabilities. | https://realpython.com/lessons/create-python-enum/#t=25.785 |
| You can use this type to create sets of named constants | https://realpython.com/lessons/create-python-enum/#t=29.265 |
| that you can use to replace literals of common data types, | https://realpython.com/lessons/create-python-enum/#t=31.805 |
| such as numbers and strings. | https://realpython.com/lessons/create-python-enum/#t=34.595 |
| 00:38 | https://realpython.com/lessons/create-python-enum/#t=38.115 |
| A classic example of when you should use an enumeration is | https://realpython.com/lessons/create-python-enum/#t=38.115 |
| when you need to create a set | https://realpython.com/lessons/create-python-enum/#t=40.955 |
| of enumerated constants representing the days of the week. | https://realpython.com/lessons/create-python-enum/#t=42.275 |
| Each day will have a symbolic name | https://realpython.com/lessons/create-python-enum/#t=46.145 |
| and a numeric value between one | https://realpython.com/lessons/create-python-enum/#t=47.815 |
| and seven inclusive. On screen | https://realpython.com/lessons/create-python-enum/#t=49.555 |
| you can see how to create this enumeration by using Enum | https://realpython.com/lessons/create-python-enum/#t=52.415 |
| as your superclass or parent class. | https://realpython.com/lessons/create-python-enum/#t=55.035 |
| 00:57 | https://realpython.com/lessons/create-python-enum/#t=57.915 |
| The | https://realpython.com/lessons/create-python-enum/#t=57.915 |
| 01:03 | https://realpython.com/lessons/create-python-enum/#t=63.335 |
| Day class is a subclass of Enum, | https://realpython.com/lessons/create-python-enum/#t=63.335 |
| so you can call Day an enumeration | https://realpython.com/lessons/create-python-enum/#t=66.285 |
| or just an Enum. Day.MONDAY, Day.TUESDAY, and the like. | https://realpython.com/lessons/create-python-enum/#t=68.645 |
| 01:13 | https://realpython.com/lessons/create-python-enum/#t=73.285 |
| Are enumeration members also known | https://realpython.com/lessons/create-python-enum/#t=73.285 |
| as Enum members or just members? | https://realpython.com/lessons/create-python-enum/#t=75.505 |
| Each member must have a value which needs to be constant. | https://realpython.com/lessons/create-python-enum/#t=78.785 |
| Often the values mapped | https://realpython.com/lessons/create-python-enum/#t=82.765 |
| to members are consecutive integer numbers, | https://realpython.com/lessons/create-python-enum/#t=84.235 |
| however, they can be of any type, | https://realpython.com/lessons/create-python-enum/#t=87.305 |
| including user-defined types. | https://realpython.com/lessons/create-python-enum/#t=89.275 |
| 01:32 | https://realpython.com/lessons/create-python-enum/#t=92.165 |
| In this example, the value of Day.MONDAY is one, | https://realpython.com/lessons/create-python-enum/#t=92.165 |
| Day.TUESDAY is two, and so on. | https://realpython.com/lessons/create-python-enum/#t=95.825 |
| Because enumeration members must be constants, | https://realpython.com/lessons/create-python-enum/#t=98.435 |
| Python doesn’t allow you to assign new values | https://realpython.com/lessons/create-python-enum/#t=101.185 |
| to members at runtime. | https://realpython.com/lessons/create-python-enum/#t=103.785 |
| 01:46 | https://realpython.com/lessons/create-python-enum/#t=106.025 |
| If you try to change the value of a member, | https://realpython.com/lessons/create-python-enum/#t=106.025 |
| you get an AttributeError. | https://realpython.com/lessons/create-python-enum/#t=108.185 |
| 01:53 | https://realpython.com/lessons/create-python-enum/#t=113.805 |
| You may have noticed that the members | https://realpython.com/lessons/create-python-enum/#t=113.805 |
| of Day are capitalized and onscreen | https://realpython.com/lessons/create-python-enum/#t=115.145 |
| you can see why. You can think of enumerations | https://realpython.com/lessons/create-python-enum/#t=117.405 |
| as collections of constants. | https://realpython.com/lessons/create-python-enum/#t=121.085 |
| 02:05 | https://realpython.com/lessons/create-python-enum/#t=125.255 |
| Like lists, tuples, | https://realpython.com/lessons/create-python-enum/#t=125.255 |
| or dictionaries, Python’s enumerations are also iterable. | https://realpython.com/lessons/create-python-enum/#t=126.685 |
| That’s why you can use list() | https://realpython.com/lessons/create-python-enum/#t=130.965 |
| to turn any enumeration into a list of enumeration members. | https://realpython.com/lessons/create-python-enum/#t=132.365 |
| 02:17 | https://realpython.com/lessons/create-python-enum/#t=137.385 |
| The members of a Python enumeration are instances | https://realpython.com/lessons/create-python-enum/#t=137.385 |
| of the container enumeration itself. | https://realpython.com/lessons/create-python-enum/#t=139.905 |
| 02:27 | https://realpython.com/lessons/create-python-enum/#t=147.635 |
| You shouldn’t confuse a custom Enum class such as Day | https://realpython.com/lessons/create-python-enum/#t=147.635 |
| with its members Day.MONDAY, Day.TUESDAY, and so on. | https://realpython.com/lessons/create-python-enum/#t=150.495 |
| 02:35 | https://realpython.com/lessons/create-python-enum/#t=155.415 |
| In this example, the Day Enum type is a hub | https://realpython.com/lessons/create-python-enum/#t=155.415 |
| for enumeration members, which happen to be of the type Day. | https://realpython.com/lessons/create-python-enum/#t=158.395 |
| Unlike member names, the name containing the enumeration | https://realpython.com/lessons/create-python-enum/#t=164.305 |
| itself isn’t a constant but a variable. | https://realpython.com/lessons/create-python-enum/#t=167.305 |
| 02:50 | https://realpython.com/lessons/create-python-enum/#t=170.065 |
| So it’s possible to rebind this name at any moment | https://realpython.com/lessons/create-python-enum/#t=170.065 |
| during your program’s execution, | https://realpython.com/lessons/create-python-enum/#t=172.885 |
| but you should avoid doing | https://realpython.com/lessons/create-python-enum/#t=174.865 |
| that as you’ve just seen. | https://realpython.com/lessons/create-python-enum/#t=176.205 |
| You’ve reassigned Day, | https://realpython.com/lessons/create-python-enum/#t=180.295 |
| which now holds a string rather than | https://realpython.com/lessons/create-python-enum/#t=181.455 |
| the original enumeration. | https://realpython.com/lessons/create-python-enum/#t=183.415 |
| 03:05 | https://realpython.com/lessons/create-python-enum/#t=185.785 |
| By doing this, you’ve lost the reference | https://realpython.com/lessons/create-python-enum/#t=185.785 |
| to the enumeration itself. | https://realpython.com/lessons/create-python-enum/#t=187.785 |
| 03:12 | https://realpython.com/lessons/create-python-enum/#t=192.315 |
| You can also use range to build enumerations | https://realpython.com/lessons/create-python-enum/#t=192.315 |
| 03:23 | https://realpython.com/lessons/create-python-enum/#t=203.745 |
| Here. | https://realpython.com/lessons/create-python-enum/#t=203.745 |
| range is used with the start and stop arguments. | https://realpython.com/lessons/create-python-enum/#t=204.045 |
| The start argument allows you to provide the number | https://realpython.com/lessons/create-python-enum/#t=207.215 |
| that starts the range, while the stop argument defines the | https://realpython.com/lessons/create-python-enum/#t=209.435 |
| number at which the range will stop generating numbers. | https://realpython.com/lessons/create-python-enum/#t=212.195 |
| 03:38 | https://realpython.com/lessons/create-python-enum/#t=218.855 |
| Even though you use the class syntax to create enumerations, | https://realpython.com/lessons/create-python-enum/#t=218.855 |
| they’re special classes | https://realpython.com/lessons/create-python-enum/#t=221.935 |
| that differ from normal Python classes. | https://realpython.com/lessons/create-python-enum/#t=223.175 |
| Unlike regular classes, Enums can’t be instantiated | https://realpython.com/lessons/create-python-enum/#t=226.075 |
| can’t be subclassed | https://realpython.com/lessons/create-python-enum/#t=230.615 |
| unless the base Enum has no members | https://realpython.com/lessons/create-python-enum/#t=231.725 |
| provide a human-readable string | https://realpython.com/lessons/create-python-enum/#t=234.595 |
| representation for their members. | https://realpython.com/lessons/create-python-enum/#t=236.325 |
| 03:58 | https://realpython.com/lessons/create-python-enum/#t=238.475 |
| They’re iterable, returning their members in a sequence. | https://realpython.com/lessons/create-python-enum/#t=238.475 |
| They provide hashable members that can be used | https://realpython.com/lessons/create-python-enum/#t=242.215 |
| as dictionary keys. | https://realpython.com/lessons/create-python-enum/#t=244.345 |
| They support the square bracket syntax, call syntax, | https://realpython.com/lessons/create-python-enum/#t=246.325 |
| and dot notation to access their members, | https://realpython.com/lessons/create-python-enum/#t=249.755 |
| and they don’t allow member reassignments. | https://realpython.com/lessons/create-python-enum/#t=252.865 |
| 04:16 | https://realpython.com/lessons/create-python-enum/#t=256.165 |
| You should keep all these subtle differences in mind when | https://realpython.com/lessons/create-python-enum/#t=256.165 |
| you start creating and working | https://realpython.com/lessons/create-python-enum/#t=258.585 |
| with your own enumerations in Python. | https://realpython.com/lessons/create-python-enum/#t=259.825 |
| While members often take consecutive integer values, | https://realpython.com/lessons/create-python-enum/#t=263.935 |
| it’s possible that the values can be of any type, | https://realpython.com/lessons/create-python-enum/#t=266.995 |
| including user-defined types. | https://realpython.com/lessons/create-python-enum/#t=269.165 |
| 04:32 | https://realpython.com/lessons/create-python-enum/#t=272.675 |
| Here’s an enumeration of school grades | https://realpython.com/lessons/create-python-enum/#t=272.675 |
| that uses non-consecutive numeric values | https://realpython.com/lessons/create-python-enum/#t=274.375 |
| in descending order. | https://realpython.com/lessons/create-python-enum/#t=276.875 |
| 04:44 | https://realpython.com/lessons/create-python-enum/#t=284.885 |
| This shows that Python enums are flexible | https://realpython.com/lessons/create-python-enum/#t=284.885 |
| and allow you to use any meaningful value for their members. | https://realpython.com/lessons/create-python-enum/#t=287.115 |
| You can set the member values according | https://realpython.com/lessons/create-python-enum/#t=290.805 |
| to the intent of your code. | https://realpython.com/lessons/create-python-enum/#t=292.345 |
| 04:56 | https://realpython.com/lessons/create-python-enum/#t=296.435 |
| You can also use string values for members. | https://realpython.com/lessons/create-python-enum/#t=296.435 |
| Here’s an example of a size enumeration | https://realpython.com/lessons/create-python-enum/#t=299.465 |
| that you can use in an online store. | https://realpython.com/lessons/create-python-enum/#t=301.525 |
| 05:12 | https://realpython.com/lessons/create-python-enum/#t=312.355 |
| The value associated with each size holds a description | https://realpython.com/lessons/create-python-enum/#t=312.355 |
| that can help you and other developers understand | https://realpython.com/lessons/create-python-enum/#t=315.215 |
| the meaning of your code. | https://realpython.com/lessons/create-python-enum/#t=317.615 |
| 05:22 | https://realpython.com/lessons/create-python-enum/#t=322.155 |
| In the next section of the course, | https://realpython.com/lessons/create-python-enum/#t=322.155 |
| you’ll take a look at the creation | https://realpython.com/lessons/create-python-enum/#t=323.395 |
| of enumerations in more depth. | https://realpython.com/lessons/create-python-enum/#t=324.815 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/videos/enumerations-intro/ |
| Overview | https://realpython.com/courses/python-enum/ |
| https://realpython.com/lessons/more-enumeration-creation/ |
|
Building Enumerations With Python's enum (Overview) 02:06
| https://realpython.com/videos/python-enum-overview/ |
|
Getting to Know Enumerations 03:41
| https://realpython.com/videos/enumerations-intro/ |
|
Creating Enumerations in Python 05:28
| https://realpython.com/lessons/create-python-enum/ |
|
Creating More Enumerations 02:53
| https://realpython.com/lessons/more-enumeration-creation/ |
|
Creating Enumerations With the Functional API 05:26
| https://realpython.com/lessons/functional-api-enumeration-creation/ |
|
Using Automatic Values, Aliases, and Unique Values 03:58
| https://realpython.com/lessons/automatic-aliases-unique/ |
|
Working With Enumerations in Python 04:37
| https://realpython.com/lessons/work-enumerations-python/ |
|
Using Enumerations in if and match Statements 02:55
| https://realpython.com/lessons/enumerations-if-match-statements/ |
|
Comparing and Sorting Enumerations 05:52
| https://realpython.com/lessons/comparing-sorting-enumerations/ |
|
Extending Enumerations With New Behavior 06:51
| https://realpython.com/lessons/extending-enumerations-behavior/ |
|
Exploring Other Enumeration Classes 07:43
| https://realpython.com/lessons/other-enumeration-classes/ |
|
Using Enumerations: Two Practical Examples 07:07
| https://realpython.com/lessons/enumeration-examples/ |
|
Building Enumerations With Python's enum (Summary) 01:20
| https://realpython.com/lessons/python-enum-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover