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 : 30

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 32The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 36The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 38

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 311 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER0The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER2

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 311 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER4The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER6

 

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

1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER81 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER9

 

Enum is hashed0

1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8Enum is hashed2

 

Enum is hashed3

1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8Enum is hashed5

 

Enum is hashed6

1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8Enum is hashed8Enum is hashed9Dog and cat are different animals Lions and cat are different0

 

Dog and cat are different animals Lions and cat are different1

1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8Enum is hashed8Dog and cat are different animals Lions and cat are different4Dog and cat are different animals Lions and cat are different0

 

Dog and cat are different animals Lions and cat are different6

1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8Enum is hashed8Dog and cat are different animals Lions and cat are different9from0

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 : 30

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 32The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 36The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 38

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 311 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER0The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER2

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 311 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER4The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER6

 

import3

1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8Enum is hashed8import6import7The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 38import9

 

Enum0

1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8Enum is hashed8Enum3Enum4Enum5Enum6

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 : 30

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 32The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 36The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 38

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 311 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER0The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER2

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 311 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER4The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER6

 

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 309 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 310The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 311 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 312

The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 311 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 315The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 316The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 317

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.

Postingan terbaru

LIHAT SEMUA