Cải thiện bài viết
Lưu bài viết
Các liệt kê trong Python được triển khai bằng cách sử dụng mô -đun có tên là Enc Enum. Enums có tên và giá trị liên quan đến chúng.enum“.Enumerations are created using classes. Enums have names and values associated with them.
Thuộc tính của Enum:
- Enums có thể được hiển thị dưới dạng chuỗi hoặc repr.string or repr.
- Enums có thể được kiểm tra các loại của họ bằng cách sử dụng loại [].type[].
- Từ khóa tên của người Viking được sử dụng để hiển thị tên của thành viên Enum.name” keyword is used to display the name of the enum member.
Ví dụ 1: Lớp Enum trong Python
Mã python để chứng minh sự liệt kê & nbsp;
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.WINTER7
Enum is hashed0
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER7
Enum is hashed2
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER7
Enum is hashed4
Enum is hashed5
Enum is hashed6
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER7
Enum is hashed4
Enum is hashed9
Enum is hashed6
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER7
Enum is hashed4
Dog and cat are different animals Lions and cat are different3
Dog and cat are different animals Lions and cat are different4
Output:
Season.SPRING SPRING 1 [, , , ]
Ví dụ 2: Truy cập chế độ & NBSP;
Các thành viên của Enum có thể được truy cập theo hai cách:
- Theo giá trị:- Trong phương pháp này, giá trị của thành viên enum được thông qua.:- In this method, the value of enum member is passed.
- Theo tên:- Trong phương pháp này, tên của thành viên enum được thông qua.:- In this method, the name of the enum member is passed.
Một giá trị hoặc tên riêng biệt cũng có thể được truy cập bằng cách sử dụng từ khóa tên của tên gọi hoặc từ khóa giá trị.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
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER7
Enum is hashed4
enum
9import
0The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 38
import
21 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER7
Enum is hashed4
import
5import
6import
7import
8Output:
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER7
Enum is hashed4
Enum is hashed5
Enum is hashed6
Ví dụ 2: Truy cập chế độ & NBSP; iterable. They can be iterated using loops
Các thành viên của Enum có thể được truy cập theo hai cách: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
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER7
Enum is hashed4
Enum is hashed5
Enum is hashed6
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.WINTER7
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 307
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 308
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 309
Output:
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER
Ví dụ 2: Truy cập chế độ & NBSP;hashing
Các thành viên của Enum có thể được truy cập theo hai cách:
Python3
Theo giá trị:- Trong phương pháp này, giá trị của thành viên enum được thông qua.
Theo tên:- Trong phương pháp này, tên của thành viên enum được thông qua.
Một giá trị hoặc tên riêng biệt cũng có thể được truy cập bằng cách sử dụng từ khóa tên của tên gọi hoặc từ khóa giá trị.
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
Ví dụ 3: liệt kê là có thể sử dụng được. Chúng có thể được lặp đi lặp lại bằng cách sử dụng các vòng
Trong ví dụ này, chúng tôi sẽ sử dụng cho Loop để in tất cả các thành viên của lớp Enum.
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 301
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3022____103
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 304
Ví dụ 4: Hỗ trợ liệt kê băm
Trong ví dụ này, chúng tôi sẽ chỉ ra cách người dùng có thể băm lớp enum có thể được sử dụng trong từ điển hoặc bộ.
import
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 : 349
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 350
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.WINTER7
Enum is hashed4
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 354
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 348
Output:
Enum is hashed
class
The enum member associated with value 2 is : SUMMER
The enum member associated with name AUTUMN is : 3
13Enumerations support two types of comparisons
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
1The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
15The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
3The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
4:- These are checked using keywords “is” and “is not“.The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
1The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
19The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
3The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
8 :- Equality comparisons of “==” and “!=” types are also supported.
Python3
Theo giá trị:- Trong phương pháp này, giá trị của thành viên enum được thông qua.
Theo tên:- Trong phương pháp này, tên của thành viên enum được thông qua.
Một giá trị hoặc tên riêng biệt cũng có thể được truy cập bằng cách sử dụng từ khóa tên của tên gọi hoặc từ khóa giá trị.
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
Ví dụ 3: liệt kê là có thể sử dụng được. Chúng có thể được lặp đi lặp lại bằng cách sử dụng các vòng
Trong ví dụ này, chúng tôi sẽ sử dụng cho Loop để in tất cả các thành viên của lớp Enum.
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.WINTER7
Enum is hashed4
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 379
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 348
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 349
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 350
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.WINTER7
Enum is hashed4
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 386
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 348
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 301
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3022____103
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 304
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.WINTER7
Enum is hashed4
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 395
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 348
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 349
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 350
Ví dụ 4: Hỗ trợ liệt kê băm
Output:
Dog and cat are different animals Lions and cat are different