Phép liệt kê trong Python được triển khai bằng cách sử dụng mô-đun có tên “enum“. Bảng liệt kê được tạo bằng cách sử dụng các lớp. Enums có tên và giá trị liên kết với chúng
Thuộc tính của enum
- Enums có thể được hiển thị dưới dạng chuỗi hoặc repr
- Enums có thể được kiểm tra các loại của chúng bằng cách sử dụng type[]
- Từ khóa “tên” được sử dụng để hiển thị tên của thành viên enum
ví dụ 1. lớp Enum trong Python
Mã Python để chứng minh liệt kê
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_______0_______3
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_______0_______3
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
0đầu ra.
Season.SPRING SPRING 1 [, , , ]
ví dụ 2. Chế độ truy cập
Thành viên Enum có thể được truy cập theo hai cách
- Theo giá trị. - Trong phương thức này, giá trị của thành viên enum được truyền vào
- Bằng tên. - Trong phương thức này, tên của thành viên enum được truyền
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” hoặc “giá trị”
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_______0_______3
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_______0_______3
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
6đầu ra.
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
ví dụ 3. Bảng liệt kê có thể lặp lại. Chúng có thể được lặp lại bằng các vòng lặp
Trong ví dụ này, chúng ta sẽ sử dụng vòng lặp for để in tất cả các thành viên của lớp Enum.
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_______0_______3
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_______0_______3
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
đầu ra.
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER
Ví dụ 4. Bảng liệt kê hỗ trợ 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ộ
Làm cách nào để truyền chuỗi thành enum?
Sử dụng Enum. Phương thức IsDefined[] để kiểm tra xem tên chuỗi hoặc giá trị số nguyên đã cho có được xác định trong một phép liệt kê đã chỉ định hay không. Do đó, việc chuyển đổi Chuỗi thành Enum có thể được thực hiện bằng cách sử dụng Enum. Phân tích cú pháp [ ] và Enum .
Làm cách nào để tạo một enum trong Python?
Phương thức này được gọi theo hai cách khác nhau. .
để tra cứu một thành viên hiện có. cls. Lớp enum được gọi. giá trị. Giá trị cần tra cứu
sử dụng cls enum để tạo một enum mới. cls. Lớp enum được gọi. giá trị. Tên của Enum mới để tạo. tên. Tên/giá trị của các thành viên cho Enum mới. mô-đun
Làm cách nào để so sánh chuỗi với tên enum?
Để so sánh một chuỗi với một enum, hãy mở rộng từ lớp str khi khai báo lớp liệt kê của bạn , e. g. lớp Màu [str, Enum]. . Sau đó, bạn sẽ có thể so sánh một chuỗi với một thành viên enum bằng cách sử dụng toán tử đẳng thức ==.
Làm cách nào để chuyển đổi chuỗi thành int Python?
Để chuyển đổi hoặc ép kiểu chuỗi thành số nguyên trong Python, bạn sử dụng hàm tích hợp int[] . Hàm nhận tham số là chuỗi ban đầu bạn muốn chuyển đổi và trả về số nguyên tương đương với giá trị bạn đã truyền. Cú pháp chung trông giống như thế này. int["str"].