Cara menggunakan python enum if

Setelah Date Time kita lanjutkan belajar python enum dan set. Enum adalah sekumpulan nama simbolik yang terikat pada nilai unik dan konstan.

Contoh enum Python

Dalam penulisan enum member, disarankan menggunakan kapital besar (UPPER_CASE) dan tentu saja harus uniqe. Tidak boleh ada anggota enum yang sama. Baik kita akan membuat sebuah contoh:

from enum import Enum
class Color(Enum):
     RED = 1
     GREEN = 2
     BLUE = 3

Class Color adalah enum atau enumerasi, dengan memiliki attribute Color.RED, Color.GREEM dan lainnya. Yang ini disebut anggota enumerasi (enum members) dan merupakan functionaly constans. Enum members memiliki nama dan value (nilai), yaitu nama dari Color.RED adalah RED dan valuenya adalah 1, dan lain lain.

Value dari members bisa berisi apa saja, int, str, atau yang lainnya. Jika nilainya tidak penting, value ini bisa sama, tapi bila penting sebaiknya diperhatikan juga.

>>> print(Color.RED)
Color.RED

>>> print(Color(1))
Color.RED

>>> print(Color['RED'])
Color.RED

Bila membuat enum member yang sama, maka akan seperti berikut ini:

class Abata(Enum):
    DURIAN = 1
    DURIAN = 2
 
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in Abata
  File "/usr/lib/python3.7/enum.py", line 101, in __setitem__
    raise TypeError('Attempted to reuse key: %r' % key)
TypeError: Attempted to reuse key: 'DURIAN'

Dan untuk value yang sama, maka akan seperti ini:

class Tryx(Enum):
    MANGGA = 1
    SEMANGKA = 1
    
print(Tryx(1))
Tryx.MANGGA

Enum juga mendukung iterasi

class Buah(Enum):
    APEL = 1
    JERUK = 2
    DUKU = 3
    MANGGA = 4
    JAMBU = 5

for buah in Buah:
    print(buah)

Buah.APEL
Buah.JERUK
Buah.DUKU
Buah.MANGGA
Buah.JAMBU

Membuat value enum otomatis

from enum import Enum, auto
class Color(Enum):
    MERAH = auto()
    PUTIH = auto()
    HITAM = auto()
    BIRU = auto()
    UNGU = auto()
    KUNING = auto()

list(Color)
[
 ,
 ,
 ,
 ,
 ,
 
]

Atau bisa juga membuat seperti ini

class AutoName(Enum):
    def _generate_next_value_(name, start, count, last_values):
            return name

class MataAngin(AutoName):
    SELATAN = auto()
    UTARA = auto()
    BARAT = auto()
    TIMUR = auto()

list(MataAngin)
[
 ,
 ,
 ,
 
]

Akhirnya

Akhirnya kita sudah mempelajari beberapa hal mengenai enum di python. Selanjutnya python type data set Mungkin kamu juga perlu membaca tulisan lainnya pada directory python

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


[, , , ]

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.