Hướng dẫn what is enum in python - enum trong python là gì

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 :  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
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
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
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
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
7
Enum is hashed
0

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
7
Enum is hashed
2

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
7
Enum is hashed
4
Enum is hashed
5
Enum is hashed
6

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
7
Enum is hashed
4
Enum is hashed
9
Enum is hashed
6

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
7
Enum is hashed
4
Dog and cat are different animals
Lions and cat are different
3
Dog and cat are different animals
Lions and cat are different
4

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 :  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
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
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
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
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
Enum is hashed
4enum 9import0
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
8import2

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
7
Enum is hashed
4import5import6import7import8

Output: 

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
7
Enum is hashed
4
Enum is hashed
5
Enum is hashed
6

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 :  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
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
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
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
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
Enum is hashed
4
Enum is hashed
5
Enum is hashed
6

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
7
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
07
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
08
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
09

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 :  3
01
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
022____103
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
04

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 :  3
11

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
49
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
50

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
7
Enum is hashed
4
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
54
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
48

Output:

Enum is hashed

class The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 313Enumerations support two types of comparisons

  • 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
    15
    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
    :- 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
    1
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    19
    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
    :- 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 :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
7
Enum is hashed
4
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
79
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
48

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
49
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
50

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
7
Enum is hashed
4
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
86
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
48

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
01
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
022____103
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
04

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
7
Enum is hashed
4
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
95
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
48

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
49
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
50

Ví dụ 4: Hỗ trợ liệt kê băm

Output: 

Dog and cat are different animals
Lions and cat are different

Enum với ví dụ là gì?

Loại Enum là một loại dữ liệu đặc biệt cho phép một biến trở thành một tập hợp các hằng số được xác định trước. Biến phải bằng một trong các giá trị đã được xác định trước cho nó. Các ví dụ phổ biến bao gồm các hướng la bàn (giá trị của Bắc, Nam, Đông và Tây) và những ngày trong tuần.a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Enum trong lớp Python là gì?

Một enum là một tập hợp các tên biểu tượng bị ràng buộc với các giá trị duy nhất.Chúng tương tự như các biến toàn cầu, nhưng chúng cung cấp một repr () hữu ích hơn, nhóm, an toàn loại và một vài tính năng khác.Như bạn có thể thấy, việc tạo một enum cũng đơn giản như viết một lớp kế thừa từ chính Enum.a set of symbolic names bound to unique values. They are similar to global variables, but they offer a more useful repr() , grouping, type-safety, and a few other features. As you can see, creating an Enum is as simple as writing a class that inherits from Enum itself.

Enum được sử dụng để làm gì?

Liệt kê hoặc enum trong C là một loại dữ liệu đặc biệt được xác định bởi người dùng.Nó bao gồm các tích phân liên tục hoặc số nguyên được người dùng đặt tên.Việc sử dụng enum trong C để đặt tên cho các giá trị số nguyên làm cho toàn bộ chương trình dễ học, hiểu và duy trì bởi cùng một lập trình viên khác nhau hoặc thậm chí khác nhau.to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

Một liệt kê trong Python là gì?

Việc liệt kê làm gì trong Python?Hàm liệt kê trong Python chuyển đổi một đối tượng thu thập dữ liệu thành một đối tượng liệt kê.Việc liệt kê trả về một đối tượng chứa một bộ đếm làm khóa cho mỗi giá trị trong một đối tượng, làm cho các mục trong bộ sưu tập dễ dàng truy cập hơn.converts a data collection object into an enumerate object. Enumerate returns an object that contains a counter as a key for each value within an object, making items within the collection easier to access.