enum

Introduction

Перечисление-это набор символических имен (членов), привязанных к уникальным, постоянным значениям.

Внутри перечисления члены могут сравниваться по идентичности

Также по ним можно итерировать (пример)

Создайте файл enum_demo.py и копируйте туда код из примеров.

Запустить файл можно командой python3 enum_demo.py

from enum import Enum # Using enum class create enumerations class Days(Enum): Mon = 1 Tue = 2 Wed = 3 Sun = 7 # print the enum member as a string print ("The enum member as a string is : ", end = ' ') print (Days.Mon) # print the enum member as a repr print ("The enum member as a repr is : ",end="") print (repr(Days.Sun)) # Check type of enum member print ("The type of enum member is : ",end ="") print (type(Days.Mon)) # print name of enum member print ("The name of enum member is : ",end ="") print (Days.Tue.name) # print value of enum member print ("The value of enum member is : ",end ="") print (Days.Tue.value) print(f"{Days.Wed} name is {Days.Wed.name} value is {Days.Wed.value}")

python enum_demo.py

The enum member as a string is : Days.Mon The enum member as a repr is : <Days.Sun: 7> The type of enum member is : <enum 'Days'> The name of enum member is : Tue The value of enum member is : 2 Days.Wed name is Wed value is 3

Перебор (итерация)

Можно перебрать члены класса циклом

for day in Days: print(f"{day} name is {day.name} value is {day.value}")

Days.Mon name is Mon value is 1 Days.Tue name is Tue value is 2 Days.Wed name is Wed value is 3 Days.Sun name is Sun value is 7

Article author: Andrei Olegovich

Похожие статьи
Interactive mode
str: strings
\: long line wrapping
Lists []
if, elif, else
Loops
Methods
Functions
*args **kwargs
enum
Get Variable Type
Testing with Python
Calling REST API with Python
Files: write, read, append, context manager…
Download File
SQLite3: database
datetime: Date and Time в Python
json.dumps
Selenium + Python
Issues with Python
DJANGO
Flask
ZPL printer script
socket :Python Sockets
Virtual Environment
subprocess: shell commands execution
multiprocessing
psutil: system usage
sys.argv: command prompt variables
PyCharm: IDE
pydantic: data validation
paramiko: SSH with Python
enumerate
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: