How to use enum in Python

7 Answers

0 votes
class Test: 
    Python, Java, C, PHP = range(4) 
  
print(Test.Python) 
print(Test.Java) 
print(Test.C)
print(Test.PHP)



'''
run:

0
1
2
3

'''

 



answered Feb 26, 2019 by avibootz
0 votes
from enum import Enum

class Color(Enum):
     RED = 1
     GREEN = 2
     BLUE = 3
     CYAN = 4
  
print(Color.RED)
print(repr(Color.RED))
print(type(Color.RED))



'''
run:

Color.RED
<Color.RED: 1>
<enum 'Color'>

'''

 



answered Feb 26, 2019 by avibootz
0 votes
from enum import Enum    

Colors = Enum('Colors', 'RED GREEN BLUE')

print(Colors.RED) 
print(Colors['GREEN'])
print(Colors.BLUE.name)



'''
run:

Colors.RED
Colors.GREEN
BLUE

'''

 



answered Feb 26, 2019 by avibootz
0 votes
from enum import Enum
 
Lang = Enum("Language", [("Python",1), ("Java",7), ("C",3), ("C++",2)])
 
print(Lang.Python.value)
print(Lang.Java.name)
  
  
  
  
'''
run:
  
1
Java
  
'''

 



answered May 5, 2024 by avibootz
0 votes
from enum import IntEnum
 
class Lang(IntEnum):
    Python = 1
    Java = 5
    C = 7
    CPP = 3
 
 
print(Lang.Python.value)
 
  
  
  
  
'''
run:
  
1
 
'''

 



answered May 5, 2024 by avibootz
0 votes
import enum
 
class Weekdays(enum.Enum):
   Sunday = 1 
   Monday = 2 
   Tuesday = 3 
   Wednesday = 4 
   Thursday = 5 
   Friday = 6 
   Saturday = 7 
 
print(Weekdays.Tuesday)  
 
print(repr(Weekdays.Sunday))  
   
print(type(Weekdays.Friday))  
 
print(Weekdays.Monday.name)  
 
 
 
'''
run:
 
Weekdays.Tuesday
<Weekdays.Sunday: 1>
<enum 'Weekdays'>
Monday

'''
 

 



answered May 5, 2024 by avibootz
0 votes
import enum
 
class Weekdays(enum.Enum):
   Sunday = 1 
   Monday = 2 
   Tuesday = 3 
   Wednesday = 4 
   Thursday = 5 
   Friday = 6 
   Saturday = 7 
 
for weekday in (Weekdays): 
    print(weekday)
 
 
 
 
'''
run:
 
Weekdays.Sunday
Weekdays.Monday
Weekdays.Tuesday
Weekdays.Wednesday
Weekdays.Thursday
Weekdays.Friday
Weekdays.Saturday
 
'''

 



answered May 5, 2024 by avibootz

Related questions

1 answer 172 views
1 answer 250 views
2 answers 232 views
1 answer 90 views
4 answers 163 views
163 views asked Jun 9, 2025 by avibootz
2 answers 95 views
95 views asked Dec 21, 2024 by avibootz
1 answer 89 views
89 views asked Dec 20, 2024 by avibootz
...