René's URL Explorer Experiment


Title: Creating Enumerations in Python (Video) – Real Python

Open Graph Title: Creating Enumerations in Python – Real Python

Description: Creating enumerations with Python’s enum Python’s Enum module provides the Enum class, which allows you to create enumeration types. To create your own enumerations, you can either subclass Enum, or use its functional API. Both options will let you…

Open Graph Description: Creating enumerations with Python’s enum Python’s Enum module provides the Enum class, which allows you to create enumeration types. To create your own enumerations, you can either subclass Enum, or use its functional API. Both options will let you…

Opengraph URL: https://realpython.com/lessons/create-python-enum/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Creating Enumerations in Python",
    "description": "Creating enumerations with Python’s enum Python’s Enum module provides the Enum class, which allows you to create enumeration types. To create your own enumerations, you can either subclass Enum, or use its functional API. Both options will let you…",
    "thumbnailUrl": ["https://files.realpython.com/media/Build-Enumerations-With-Pythons-enum_Watermarked.bbcd46a82f58.jpg"],
    "uploadDate": "2024-01-30T14:00:00+00:00",
    "duration": "PT5M28S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/create-python-enum/#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/create-python-enum/
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/create-python-enum/
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%2Fcreate-python-enum%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/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
Transcripthttps://realpython.com/lessons/create-python-enum/#transcript
Discussionhttps://realpython.com/lessons/create-python-enum/#discussion
00:00https://realpython.com/lessons/create-python-enum/#t=0.875
Creating enumerations with Python’s enumhttps://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:10https://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 constantshttps://realpython.com/lessons/create-python-enum/#t=16.205
as Enum members.https://realpython.com/lessons/create-python-enum/#t=19.135
00:22https://realpython.com/lessons/create-python-enum/#t=22.615
The Enum module defines a general purpose enumeration typehttps://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 constantshttps://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:38https://realpython.com/lessons/create-python-enum/#t=38.115
A classic example of when you should use an enumeration ishttps://realpython.com/lessons/create-python-enum/#t=38.115
when you need to create a sethttps://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 namehttps://realpython.com/lessons/create-python-enum/#t=46.145
and a numeric value between onehttps://realpython.com/lessons/create-python-enum/#t=47.815
and seven inclusive. On screenhttps://realpython.com/lessons/create-python-enum/#t=49.555
you can see how to create this enumeration by using Enumhttps://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:57https://realpython.com/lessons/create-python-enum/#t=57.915
Thehttps://realpython.com/lessons/create-python-enum/#t=57.915
01:03https://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 enumerationhttps://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:13https://realpython.com/lessons/create-python-enum/#t=73.285
Are enumeration members also knownhttps://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 mappedhttps://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:32https://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 valueshttps://realpython.com/lessons/create-python-enum/#t=101.185
to members at runtime.https://realpython.com/lessons/create-python-enum/#t=103.785
01:46https://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:53https://realpython.com/lessons/create-python-enum/#t=113.805
You may have noticed that the membershttps://realpython.com/lessons/create-python-enum/#t=113.805
of Day are capitalized and onscreenhttps://realpython.com/lessons/create-python-enum/#t=115.145
you can see why. You can think of enumerationshttps://realpython.com/lessons/create-python-enum/#t=117.405
as collections of constants.https://realpython.com/lessons/create-python-enum/#t=121.085
02:05https://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:17https://realpython.com/lessons/create-python-enum/#t=137.385
The members of a Python enumeration are instanceshttps://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:27https://realpython.com/lessons/create-python-enum/#t=147.635
You shouldn’t confuse a custom Enum class such as Dayhttps://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:35https://realpython.com/lessons/create-python-enum/#t=155.415
In this example, the Day Enum type is a hubhttps://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 enumerationhttps://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:50https://realpython.com/lessons/create-python-enum/#t=170.065
So it’s possible to rebind this name at any momenthttps://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 doinghttps://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 thanhttps://realpython.com/lessons/create-python-enum/#t=181.455
the original enumeration.https://realpython.com/lessons/create-python-enum/#t=183.415
03:05https://realpython.com/lessons/create-python-enum/#t=185.785
By doing this, you’ve lost the referencehttps://realpython.com/lessons/create-python-enum/#t=185.785
to the enumeration itself.https://realpython.com/lessons/create-python-enum/#t=187.785
03:12https://realpython.com/lessons/create-python-enum/#t=192.315
You can also use range to build enumerationshttps://realpython.com/lessons/create-python-enum/#t=192.315
03:23https://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 numberhttps://realpython.com/lessons/create-python-enum/#t=207.215
that starts the range, while the stop argument defines thehttps://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:38https://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 classeshttps://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 instantiatedhttps://realpython.com/lessons/create-python-enum/#t=226.075
can’t be subclassedhttps://realpython.com/lessons/create-python-enum/#t=230.615
unless the base Enum has no membershttps://realpython.com/lessons/create-python-enum/#t=231.725
provide a human-readable stringhttps://realpython.com/lessons/create-python-enum/#t=234.595
representation for their members.https://realpython.com/lessons/create-python-enum/#t=236.325
03:58https://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 usedhttps://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:16https://realpython.com/lessons/create-python-enum/#t=256.165
You should keep all these subtle differences in mind whenhttps://realpython.com/lessons/create-python-enum/#t=256.165
you start creating and workinghttps://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:32https://realpython.com/lessons/create-python-enum/#t=272.675
Here’s an enumeration of school gradeshttps://realpython.com/lessons/create-python-enum/#t=272.675
that uses non-consecutive numeric valueshttps://realpython.com/lessons/create-python-enum/#t=274.375
in descending order.https://realpython.com/lessons/create-python-enum/#t=276.875
04:44https://realpython.com/lessons/create-python-enum/#t=284.885
This shows that Python enums are flexiblehttps://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 accordinghttps://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:56https://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 enumerationhttps://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:12https://realpython.com/lessons/create-python-enum/#t=312.355
The value associated with each size holds a descriptionhttps://realpython.com/lessons/create-python-enum/#t=312.355
that can help you and other developers understandhttps://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:22https://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 creationhttps://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 Memberhttps://realpython.com/account/join/
https://realpython.com/videos/enumerations-intro/
Overviewhttps://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 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.