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
enum
import
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 : 31
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 32
The 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 : 31
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 36
The 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 : 31
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER0
The 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 : 31
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER4
The 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.WINTER8
1 - 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.WINTER8
Enum is hashed2
Enum is hashed3
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8
Enum is hashed5
Enum is hashed6
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8
Enum is hashed8
Enum is hashed9
Dog 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.WINTER8
Enum is hashed8
Dog and cat are different animals Lions and cat are different4
Dog 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.WINTER8
Enum is hashed8
Dog and cat are different animals Lions and cat are different9
from
0Output:
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
enum
import
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 : 31
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 32
The 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 : 31
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 36
The 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 : 31
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER0
The 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 : 31
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER4
The 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
import
3
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8
Enum is hashed8
import
6import
7The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 38
import
9
Enum
0
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8
Enum is hashed8
Enum
3Enum
4Enum
5Enum
6Output:
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
enum
import
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 : 31
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 32
The 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 : 31
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 36
The 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 : 31
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER0
The 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 : 31
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER4
The 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 : 310
The 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 : 31
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 315
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 316
The 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.