Python enum từ chuỗi

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 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_______0_______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_______0_______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

đầ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 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_______0_______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_______0_______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

đầ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 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_______0_______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_______0_______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

đầ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").