1import enum
2# Using enum class create enumerations
3class Days(enum.Enum):
4 Sun = 1
5 Mon = 2
6 Tue = 3
7# print the enum member as a string
8print ("The enum member as a string is : ",end="")
9print (Days.Mon)
10
11# print the enum member as a repr
12print ("he enum member as a repr is : ",end="")
13print (repr(Days.Sun))
14
15# Check type of enum member
16print ("The type of enum member is : ",end ="")
17print (type(Days.Mon))
18
19# print name of enum member
20print ("The name of enum member is : ",end ="")
21print (Days.Tue.name)
1const seasons = {
2 SUMMER: {
3 BEGINNING: "summer.beginning",
4 ENDING: "summer.ending"
5 },
6 WINTER: "winter",
7 SPRING: "spring",
8 AUTUMN: "autumn"
9};
10
11let season = seasons.SUMMER.BEGINNING;
12
13if (!season) {
14 throw new Error("Season is not defined");
15}
16
17switch (season) {
18 case seasons.SUMMER.BEGINNING:
19 // Do something for summer beginning
20 case seasons.SUMMER.ENDING:
21 // Do something for summer ending
22 case seasons.SUMMER:
23 // This will work if season = seasons.SUMMER
24 // Do something for summer (generic)
25 case seasons.WINTER:
26 //Do something for winter
27 case seasons.SPRING:
28 //Do something for spring
29 case seasons.AUTUMN:
30 //Do something for autumn
31}
1python3 -m venv tutorial-env
2tutorial-env\Scripts\activate.bat (window)
3source tutorial-env/bin/activate (linux)
1>>> from enum import Enum
2>>> class Color(Enum):
3... RED = 1
4... GREEN = 2
5... BLUE = 3
6...
7
1import enum
2
3class Days(enum.Enum):
4 Sun = 1
5 Mon = 2
6 Tue = 3
7
8print ("The enum members are : ")
9
10for weekday in (Days):
11 print(weekday)
12
13# The enum members are :
14# Days.Sun
15# Days.Mon
16# Days.Tue
17
18best_day = Days.Sun
19worst_day = Days(2)
20
21print(best_day) # Days.Sun
22print(worst_day) # Days.Mon