René's URL Explorer Experiment


Title: Working With Enumerations in Python (Video) – Real Python

Open Graph Title: Working With Enumerations in Python – Real Python

Description: Working with Enumerations in Python. When it comes to using enumerations in your code, accessing their members is a fundamental operation to perform. You’ll have three different ways to access enumeration members in Python. Let’s say you need to…

Open Graph Description: Working with Enumerations in Python. When it comes to using enumerations in your code, accessing their members is a fundamental operation to perform. You’ll have three different ways to access enumeration members in Python. Let’s say you need to…

Opengraph URL: https://realpython.com/lessons/work-enumerations-python/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Working With Enumerations in Python",
    "description": "Working with Enumerations in Python. When it comes to using enumerations in your code, accessing their members is a fundamental operation to perform. You’ll have three different ways to access enumeration members in Python. Let’s say you need to…",
    "thumbnailUrl": ["https://files.realpython.com/media/Build-Enumerations-With-Pythons-enum_Watermarked.bbcd46a82f58.jpg"],
    "uploadDate": "2024-01-30T14:00:00+00:00",
    "duration": "PT4M37S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/work-enumerations-python/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/Build-Enumerations-With-Pythons-enum_Watermarked.bbcd46a82f58.jpg
og:imagehttps://files.realpython.com/media/Build-Enumerations-With-Pythons-enum_Watermarked.bbcd46a82f58.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/lessons/work-enumerations-python/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/lessons/work-enumerations-python/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Flessons%2Fwork-enumerations-python%2F
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-enum
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-enum
https://realpython.com/courses/python-enum/#team
Building Enumerations With Python's enumhttps://realpython.com/courses/python-enum/
Darren Joneshttps://realpython.com/courses/python-enum/#team
Recommended Tutorialhttps://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 Questionhttps://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
Transcripthttps://realpython.com/lessons/work-enumerations-python/#transcript
Discussionhttps://realpython.com/lessons/work-enumerations-python/#discussion
00:00https://realpython.com/lessons/work-enumerations-python/#t=0.54
Working with Enumerations in Python. When it comeshttps://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 fundamentalhttps://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 wayshttps://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:16https://realpython.com/lessons/work-enumerations-python/#t=16.165
Let’s say you need to access the north memberhttps://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:23https://realpython.com/lessons/work-enumerations-python/#t=23.875
This shows how you can access an Enum member using the dothttps://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 memberhttps://realpython.com/lessons/work-enumerations-python/#t=31.235
by calling the enumerationhttps://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:37https://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 flexibilityhttps://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 approachhttps://realpython.com/lessons/work-enumerations-python/#t=48.325
in Python code, but the other two approacheshttps://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:56https://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 enumerationhttps://realpython.com/lessons/work-enumerations-python/#t=61.635
with a member’s valuehttps://realpython.com/lessons/work-enumerations-python/#t=64.195
as an argument can make you feel like you arehttps://realpython.com/lessons/work-enumerations-python/#t=65.265
instantiating the enumeration.https://realpython.com/lessons/work-enumerations-python/#t=67.475
01:10https://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:18https://realpython.com/lessons/work-enumerations-python/#t=78.095
Trying to create an instancehttps://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 membershttps://realpython.com/lessons/work-enumerations-python/#t=84.055
through an enumeration call.https://realpython.com/lessons/work-enumerations-python/#t=87.275
01:32https://realpython.com/lessons/work-enumerations-python/#t=92.095
The members of a Python enumeration are instanceshttps://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 automaticallyhttps://realpython.com/lessons/work-enumerations-python/#t=96.575
provided with a .name attribute that holds the member’s namehttps://realpython.com/lessons/work-enumerations-python/#t=100.795
as a string. Members also get a .value attributehttps://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:49https://realpython.com/lessons/work-enumerations-python/#t=109.575
In the class definition, you can access .namehttps://realpython.com/lessons/work-enumerations-python/#t=109.575
and .value as you would dohttps://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:06https://realpython.com/lessons/work-enumerations-python/#t=126.985
The .name and .value attributeshttps://realpython.com/lessons/work-enumerations-python/#t=126.985
of an Enum member give you direct accesshttps://realpython.com/lessons/work-enumerations-python/#t=128.465
to the member’s name as a stringhttps://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:15https://realpython.com/lessons/work-enumerations-python/#t=135.095
These attributes come in handy when you’re iteratinghttps://realpython.com/lessons/work-enumerations-python/#t=135.095
through your enumerations, which you’ll explorehttps://realpython.com/lessons/work-enumerations-python/#t=137.605
next. One useful featurehttps://realpython.com/lessons/work-enumerations-python/#t=140.075
of Python’s enumerations compared to regular classes ishttps://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:30https://realpython.com/lessons/work-enumerations-python/#t=150.855
Because of this, you can use them in for loopshttps://realpython.com/lessons/work-enumerations-python/#t=150.855
and with other tools that accepthttps://realpython.com/lessons/work-enumerations-python/#t=153.465
and process iterable objects. Python’s enumerations supporthttps://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:48https://realpython.com/lessons/work-enumerations-python/#t=168.455
In this example, you use a for loophttps://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 orderhttps://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:59https://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:09https://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 enumerationshttps://realpython.com/lessons/work-enumerations-python/#t=195.885
using this technique will be a straightforward taskhttps://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:23https://realpython.com/lessons/work-enumerations-python/#t=203.245
Alternatively, enumerations have a special attribute calledhttps://realpython.com/lessons/work-enumerations-python/#t=203.245
.__members__ that you can also usehttps://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 dictionaryhttps://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:35https://realpython.com/lessons/work-enumerations-python/#t=215.815
The difference between iterating over this dictionaryhttps://realpython.com/lessons/work-enumerations-python/#t=215.815
and the enumeration directly ishttps://realpython.com/lessons/work-enumerations-python/#t=218.075
that the dictionary gives you access to all membershttps://realpython.com/lessons/work-enumerations-python/#t=219.915
of the enumeration, including allhttps://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:46https://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 applyhttps://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:58https://realpython.com/lessons/work-enumerations-python/#t=238.365
Some of these techniques include using dictionary methodshttps://realpython.com/lessons/work-enumerations-python/#t=238.365
such as .keys()https://realpython.com/lessons/work-enumerations-python/#t=240.845
04:06https://realpython.com/lessons/work-enumerations-python/#t=246.685
.values()https://realpython.com/lessons/work-enumerations-python/#t=246.685
04:16https://realpython.com/lessons/work-enumerations-python/#t=256.055
and .items().https://realpython.com/lessons/work-enumerations-python/#t=256.055
04:30https://realpython.com/lessons/work-enumerations-python/#t=270.365
In the next section of the course, you’ll look at howhttps://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 Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/automatic-aliases-unique/
Overviewhttps://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 Policyhttps://realpython.com/privacy-policy/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.