|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/work-enumerations-python/ |
| 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/work-enumerations-python/ |
| 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%2Fwork-enumerations-python%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/work-enumerations-python/#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/work-enumerations-python/#transcript |
| Discussion | https://realpython.com/lessons/work-enumerations-python/#discussion |
| 00:00 | https://realpython.com/lessons/work-enumerations-python/#t=0.54 |
| Working with Enumerations in Python. When it comes | https://realpython.com/lessons/work-enumerations-python/#t=0.54 |
| to using enumerations in your code, | https://realpython.com/lessons/work-enumerations-python/#t=4.825 |
| accessing their members is a fundamental | https://realpython.com/lessons/work-enumerations-python/#t=6.8149999999999995 |
| operation to perform. | https://realpython.com/lessons/work-enumerations-python/#t=8.905 |
| You’ll have three different ways | https://realpython.com/lessons/work-enumerations-python/#t=10.865 |
| to access enumeration members in Python. | https://realpython.com/lessons/work-enumerations-python/#t=12.265 |
| 00:16 | https://realpython.com/lessons/work-enumerations-python/#t=16.165 |
| Let’s say you need to access the north member | https://realpython.com/lessons/work-enumerations-python/#t=16.165 |
| of the Cardinal direction enumeration on screen. | https://realpython.com/lessons/work-enumerations-python/#t=18.235 |
| 00:23 | https://realpython.com/lessons/work-enumerations-python/#t=23.875 |
| This shows how you can access an Enum member using the dot | https://realpython.com/lessons/work-enumerations-python/#t=23.875 |
| notation, which is pretty intuitive and readable. | https://realpython.com/lessons/work-enumerations-python/#t=26.965 |
| This line accesses the target member | https://realpython.com/lessons/work-enumerations-python/#t=31.235 |
| by calling the enumeration | https://realpython.com/lessons/work-enumerations-python/#t=33.105 |
| with the member’s value as an argument. | https://realpython.com/lessons/work-enumerations-python/#t=34.405 |
| 00:37 | https://realpython.com/lessons/work-enumerations-python/#t=37.565 |
| And a third access method is to use subscript notation. | https://realpython.com/lessons/work-enumerations-python/#t=37.565 |
| Python’s Enumerations offer great flexibility | https://realpython.com/lessons/work-enumerations-python/#t=43.235 |
| for you to access members. | https://realpython.com/lessons/work-enumerations-python/#t=46.015 |
| The dot notation is arguably the most commonly used approach | https://realpython.com/lessons/work-enumerations-python/#t=48.325 |
| in Python code, but the other two approaches | https://realpython.com/lessons/work-enumerations-python/#t=51.525 |
| can be helpful as well. | https://realpython.com/lessons/work-enumerations-python/#t=54.485 |
| 00:56 | https://realpython.com/lessons/work-enumerations-python/#t=56.085 |
| So use the notation that fulfills your specific needs, | https://realpython.com/lessons/work-enumerations-python/#t=56.085 |
| conventions, and style. | https://realpython.com/lessons/work-enumerations-python/#t=59.095 |
| It’s important to note that calling an enumeration | https://realpython.com/lessons/work-enumerations-python/#t=61.635 |
| with a member’s value | https://realpython.com/lessons/work-enumerations-python/#t=64.195 |
| as an argument can make you feel like you are | https://realpython.com/lessons/work-enumerations-python/#t=65.265 |
| instantiating the enumeration. | https://realpython.com/lessons/work-enumerations-python/#t=67.475 |
| 01:10 | https://realpython.com/lessons/work-enumerations-python/#t=70.055 |
| But as you already know, enumerations can’t be instantiated. | https://realpython.com/lessons/work-enumerations-python/#t=70.055 |
| 01:18 | https://realpython.com/lessons/work-enumerations-python/#t=78.095 |
| Trying to create an instance | https://realpython.com/lessons/work-enumerations-python/#t=78.095 |
| of an existing enumeration isn’t allowed, | https://realpython.com/lessons/work-enumerations-python/#t=79.475 |
| so you get a TypeError if you attempt to do it. | https://realpython.com/lessons/work-enumerations-python/#t=81.655 |
| So you mustn’t confuse instantiating with accessing members | https://realpython.com/lessons/work-enumerations-python/#t=84.055 |
| through an enumeration call. | https://realpython.com/lessons/work-enumerations-python/#t=87.275 |
| 01:32 | https://realpython.com/lessons/work-enumerations-python/#t=92.095 |
| The members of a Python enumeration are instances | https://realpython.com/lessons/work-enumerations-python/#t=92.095 |
| of their containing class. | https://realpython.com/lessons/work-enumerations-python/#t=94.615 |
| During the Enum class parsing, each member is automatically | https://realpython.com/lessons/work-enumerations-python/#t=96.575 |
| provided with a .name attribute that holds the member’s name | https://realpython.com/lessons/work-enumerations-python/#t=100.795 |
| as a string. Members also get a .value attribute | https://realpython.com/lessons/work-enumerations-python/#t=103.375 |
| that stores the value assigned to the member itself. | https://realpython.com/lessons/work-enumerations-python/#t=106.905 |
| 01:49 | https://realpython.com/lessons/work-enumerations-python/#t=109.575 |
| In the class definition, you can access .name | https://realpython.com/lessons/work-enumerations-python/#t=109.575 |
| and .value as you would do | https://realpython.com/lessons/work-enumerations-python/#t=112.945 |
| with a regular attribute using the dot notation. | https://realpython.com/lessons/work-enumerations-python/#t=114.365 |
| Take a look at this example, which simulates a semaphore, | https://realpython.com/lessons/work-enumerations-python/#t=118.895 |
| commonly known as a traffic light. | https://realpython.com/lessons/work-enumerations-python/#t=122.425 |
| 02:06 | https://realpython.com/lessons/work-enumerations-python/#t=126.985 |
| The .name and .value attributes | https://realpython.com/lessons/work-enumerations-python/#t=126.985 |
| of an Enum member give you direct access | https://realpython.com/lessons/work-enumerations-python/#t=128.465 |
| to the member’s name as a string | https://realpython.com/lessons/work-enumerations-python/#t=130.565 |
| and to the member’s value, respectively. | https://realpython.com/lessons/work-enumerations-python/#t=132.465 |
| 02:15 | https://realpython.com/lessons/work-enumerations-python/#t=135.095 |
| These attributes come in handy when you’re iterating | https://realpython.com/lessons/work-enumerations-python/#t=135.095 |
| through your enumerations, which you’ll explore | https://realpython.com/lessons/work-enumerations-python/#t=137.605 |
| next. One useful feature | https://realpython.com/lessons/work-enumerations-python/#t=140.075 |
| of Python’s enumerations compared to regular classes is | https://realpython.com/lessons/work-enumerations-python/#t=144.515 |
| that enumerations are iterable by default. | https://realpython.com/lessons/work-enumerations-python/#t=147.735 |
| 02:30 | https://realpython.com/lessons/work-enumerations-python/#t=150.855 |
| Because of this, you can use them in for loops | https://realpython.com/lessons/work-enumerations-python/#t=150.855 |
| and with other tools that accept | https://realpython.com/lessons/work-enumerations-python/#t=153.465 |
| and process iterable objects. Python’s enumerations support | https://realpython.com/lessons/work-enumerations-python/#t=155.045 |
| direct iteration over members in the definition order. | https://realpython.com/lessons/work-enumerations-python/#t=159.855 |
| 02:48 | https://realpython.com/lessons/work-enumerations-python/#t=168.455 |
| In this example, you use a for loop | https://realpython.com/lessons/work-enumerations-python/#t=168.455 |
| to iterate over the members of Flavor. | https://realpython.com/lessons/work-enumerations-python/#t=170.655 |
| Note that members are produced in the same order | https://realpython.com/lessons/work-enumerations-python/#t=173.495 |
| as they were defined in the class definition. | https://realpython.com/lessons/work-enumerations-python/#t=175.845 |
| 02:59 | https://realpython.com/lessons/work-enumerations-python/#t=179.165 |
| When you are iterating over an enumeration, | https://realpython.com/lessons/work-enumerations-python/#t=179.165 |
| you can access the .name and .value attributes as you go. | https://realpython.com/lessons/work-enumerations-python/#t=181.395 |
| 03:09 | https://realpython.com/lessons/work-enumerations-python/#t=189.925 |
| This looks pretty similar to iterating over a dictionary, | https://realpython.com/lessons/work-enumerations-python/#t=189.925 |
| so if you are familiar with dictionary iteration, | https://realpython.com/lessons/work-enumerations-python/#t=193.555 |
| then looping over enumerations | https://realpython.com/lessons/work-enumerations-python/#t=195.885 |
| using this technique will be a straightforward task | https://realpython.com/lessons/work-enumerations-python/#t=197.585 |
| with many potential use cases. | https://realpython.com/lessons/work-enumerations-python/#t=199.815 |
| 03:23 | https://realpython.com/lessons/work-enumerations-python/#t=203.245 |
| Alternatively, enumerations have a special attribute called | https://realpython.com/lessons/work-enumerations-python/#t=203.245 |
| .__members__ that you can also use | https://realpython.com/lessons/work-enumerations-python/#t=206.635 |
| for iterating over their members. | https://realpython.com/lessons/work-enumerations-python/#t=208.675 |
| This attribute holds a dictionary | https://realpython.com/lessons/work-enumerations-python/#t=211.495 |
| that maps names to members. | https://realpython.com/lessons/work-enumerations-python/#t=213.105 |
| 03:35 | https://realpython.com/lessons/work-enumerations-python/#t=215.815 |
| The difference between iterating over this dictionary | https://realpython.com/lessons/work-enumerations-python/#t=215.815 |
| and the enumeration directly is | https://realpython.com/lessons/work-enumerations-python/#t=218.075 |
| that the dictionary gives you access to all members | https://realpython.com/lessons/work-enumerations-python/#t=219.915 |
| of the enumeration, including all | https://realpython.com/lessons/work-enumerations-python/#t=222.235 |
| of the aliases that you may have. | https://realpython.com/lessons/work-enumerations-python/#t=224.315 |
| 03:46 | https://realpython.com/lessons/work-enumerations-python/#t=226.865 |
| Because .__members__ holds a regular dictionary, | https://realpython.com/lessons/work-enumerations-python/#t=226.865 |
| you can use all the iteration techniques that apply | https://realpython.com/lessons/work-enumerations-python/#t=229.615 |
| to this built-in data type. | https://realpython.com/lessons/work-enumerations-python/#t=232.115 |
| 03:58 | https://realpython.com/lessons/work-enumerations-python/#t=238.365 |
| Some of these techniques include using dictionary methods | https://realpython.com/lessons/work-enumerations-python/#t=238.365 |
| such as .keys() | https://realpython.com/lessons/work-enumerations-python/#t=240.845 |
| 04:06 | https://realpython.com/lessons/work-enumerations-python/#t=246.685 |
| .values() | https://realpython.com/lessons/work-enumerations-python/#t=246.685 |
| 04:16 | https://realpython.com/lessons/work-enumerations-python/#t=256.055 |
| and .items(). | https://realpython.com/lessons/work-enumerations-python/#t=256.055 |
| 04:30 | https://realpython.com/lessons/work-enumerations-python/#t=270.365 |
| In the next section of the course, you’ll look at how | https://realpython.com/lessons/work-enumerations-python/#t=270.365 |
| to use enumerations in if and match statements. | https://realpython.com/lessons/work-enumerations-python/#t=272.545 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/automatic-aliases-unique/ |
| Overview | https://realpython.com/courses/python-enum/ |
| https://realpython.com/lessons/enumerations-if-match-statements/ |
|
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