Cara menggunakan python enum multiple values

Enumerations in Python are implemented by using the module named “enum“.Enumerations are created using classes. Enums have names and values associated with them.

Properties of enum:

  • Enums can be displayed as string or repr.
  • Enums can be checked for their types using type().
  • The “name” keyword is used to display the name of the enum member.

Example 1: Enum class in Python

Python code to demonstrate enumerations 

Python3




from enumimport Enum

 

class

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
0

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
2
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
4

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
6
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
8

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
0
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
2

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
4
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
6

 

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
7

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
9

 

Enum is hashed
0

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
2

 

Enum is hashed
3

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
5

 

Enum is hashed
6

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
8
Enum is hashed
9
Dog and cat are different animals
Lions and cat are different
0

 

Dog and cat are different animals
Lions and cat are different
1

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
8
Dog and cat are different animals
Lions and cat are different
4
Dog and cat are different animals
Lions and cat are different
0

 

Dog and cat are different animals
Lions and cat are different
6

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
8
Dog and cat are different animals
Lions and cat are different
9from0

Output: 

Season.SPRING
SPRING
1
<enum 'Season'>
<Season.SPRING: 1>
[<Season.SPRING: 1>, <Season.SUMMER: 2>, <Season.AUTUMN: 3>, <Season.WINTER: 4>]

Example 2: Accessing Modes 

Enum members can be accessed in two ways:

  • By value:- In this method, the value of enum member is passed.
  • By name:- In this method, the name of the enum member is passed.

A separate value or name can also be accessed using the “name” or “value” keyword.

Python3




from enumimport Enum

 

class

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
0

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
2
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
4

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
6
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
8

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
0
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
2

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
4
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
6

 

import3

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
8import6import7
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
8import9

 

Enum0

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
8Enum3Enum4Enum5Enum6

Output: 

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3

Example 3: Enumerations are iterable. They can be iterated using loops

In this example, we will use for loop to print all the members of the Enum class.

Python3




from enumimport Enum

 

class

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
0

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
2
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
4

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
6
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
8

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
0
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
2

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
4
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
6

 

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
09
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
10
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
11
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
12

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
15
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
16
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
17

Output: 

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER

Example 4: Enumerations support hashing

In this example, we will show how users can hash the Enum class that can be used in dictionaries or sets.