Setelah Date Time kita lanjutkan belajar python enum dan set. Enum adalah sekumpulan nama simbolik yang terikat pada nilai unik dan konstan. Show Contoh enum PythonDalam 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:
Class 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.
Bila membuat enum member yang sama, maka akan seperti berikut ini:
Dan untuk value yang sama, maka akan seperti ini:
Enum juga mendukung iterasi
Membuat value enum otomatis
Atau bisa juga membuat seperti ini
AkhirnyaAkhirnya 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:
Example 1: Enum class in PythonPython code to demonstrate enumerations Python3
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 <enum 'Season'> <Season.SPRING: 1> [<Season.SPRING: 1>, <Season.SUMMER: 2>, <Season.AUTUMN: 3>, <Season.WINTER: 4>] Example 2: Accessing ModesEnum members can be accessed in two ways:
A separate value or name can also be accessed using the “name” or “value” keyword. Python3
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.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
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 loopsIn this example, we will use for loop to print all the members of the Enum class. Python3
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 hashingIn this example, we will show how users can hash the Enum class that can be used in dictionaries or sets. |