Một dict có thể cung cấp ánh xạ giữa biểu tượng và hàm. Mô-đun operator
cung cấp quyền truy cập thuận tiện vào các hàm tích hợp được sử dụng trong các biểu thức. Ngoài ra, bạn có thể bảo vệ một số thuộc tính khỏi việc vô tình được sửa đổi bằng mã lỗi bằng cách làm cho chúng chỉ các thuộc tính chỉ đọc. Cố gắng sửa đổi chúng sẽ nâng cao một thuộc tính trong thời gian chạy. Nếu ai đó thực sự cần Viết truy cập, nó vẫn có sẵn thông qua các biến số tiền tố nhấn mạnh, nhưng đó là giao diện riêng.
import operator
class Expression[object]:
_OP_MAP = dict[[x, getattr[operator, y]] for x, y in
[['*', 'mul'], ['/', 'truediv'], ['//', 'floordiv'],
['+', 'add'], ['-', 'sub']]]
_OP_MAP[None] = lambda a, b: a
a = property[lambda self: self._a] #read-only interface
b = property[lambda self: self._b]
op = property[lambda self: self._op]
def __init__[self, value1=0, value2=0, operation=None]:
self._a = value1 #mutable -- we're all adults
self._b = value2
self._op = self._OP_MAP[operation]
def parse_string[self, expression_string, variables]:
#...
self._op = self._OP_MAP.get[operator] #defaults to None
def evaluate[self]:
return self.op[self.a, self.b]
def __repr__[self]:
for k, v in self._OP_MAP.items[]:
if self.op == v:
return '{0} {1} {2}'.format[self.a, k, self.b]
import animal
animal.Cat
42 import animal
animal.Cat
33Season.SPRING SPRING 1 [, , , ]0
Season.SPRING SPRING 1 [, , , ]0
import animal
animal.Cat
46import animal
animal.Cat
38import animal
animal.Cat
48import animal
animal.Cat
41import animal
animal.Cat
50Ví dụ 5: Các liệt kê hỗ trợ hai loại so sánh
Danh tính:- Chúng được kiểm tra bằng cách sử dụng từ khóa, là và không phải là người.
Mã không có Enumvà tham khảo nó như thế này
import animal
animal.Cat
Enum cố gắng giải quyết vấn đề này bằng cách nhóm các hằng số này một cách hợp lý.
Trong Python, chúng ta có thể tạo ra một enum như thế này
Bây giờ chúng tôi đã nhóm động vật theo loại enum. Lưu ý rằng chúng tôi đã sử dụng lớp cơ sở Enum.
Thuộc tính name
cho biết tên của thuộc tính Enum và
import animal
animal.Cat
0 cho giá trị Enum. Chúng ta có thể sử dụng phương thức import animal
animal.Cat
1 nếu chúng ta không muốn gán giá trị cho enum. Trong trường hợp đó, giá trị mặc định sẽ là 1 và nó sẽ được tự động kích hoạt mỗi khi nó được gọi.Chúng tôi cũng có thể lưu trữ các thuộc tính của động vật như là một phần của enum. Xem xét các ví dụ sau.
Ở đây chúng tôi đã thêm một số chi tiết cho động vật enum. Trường đầu tiên là vòng đời và lĩnh vực thứ hai là tên khoa học của nó. Để thêm các giá trị bổ sung, chúng ta nên khai báo một hàm tạo và thành viên có thể được truy cập. Chúng tôi thậm chí có thể khai báo các phương pháp bổ sung.
Có một số biến thể của enum như intenum, intflag và cờ. Bạn có thể khám phá nó ở đây //docs.python.org/3/l Library/enum.html 🙂
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọcenum“.Enumerations are created using classes. Enums have names and values associated with them.
Bàn luận
- 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.string or repr.
- Thuộc tính của Enum:type[].
- Enums có thể được hiển thị dưới dạng chuỗi hoặc repr.name” keyword is used to display the name of the enum member.
Enums có thể được kiểm tra các loại của họ bằng cách sử dụng loại [].
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.
Python3
Ví dụ 1: Lớp Enum trong Python
Mã python để chứng minh sự liệt kê & nbsp;
import animal
animal.Cat
2 import animal
animal.Cat
3import animal
animal.Cat
4 import animal
animal.Cat
5import animal
animal.Cat
6 import animal
animal.Cat
7import animal
animal.Cat
8import animal
animal.Cat
9Season.SPRING SPRING 1 [, , , ]0
Season.SPRING SPRING 1 [, , , ]1
import animal
animal.Cat
8Season.SPRING SPRING 1 [, , , ]3
Season.SPRING SPRING 1 [, , , ]0
Season.SPRING SPRING 1 [, , , ]5
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 : 35
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 : 37
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 : 39
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER1
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER2
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER3
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER1
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER6
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER3
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER1
Enum is hashed0
Enum is hashed1
Output:
Season.SPRING SPRING 1 [, , , ]
import animal
animal.Cat
8Season.SPRING
SPRING
1
[, , , ]
7Season.SPRING
SPRING
1
[, , , ]
0 Season.SPRING
SPRING
1
[, , , ]
9
animal.Cat
import animal
animal.Cat
8The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31
Season.SPRING SPRING 1 [, , , ]0
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33
- Ví dụ 2: Truy cập chế độ & NBSP;:- In this method, the value of enum member is passed.
- Các thành viên của Enum có thể được truy cập theo hai cách::- In this method, the name of the enum member is passed.
Theo giá trị:- Trong phương pháp này, giá trị của thành viên enum được thông qua.name” or “value” keyword.
Python3
Ví dụ 1: Lớp Enum trong Python
Mã python để chứng minh sự liệt kê & nbsp;
import animal
animal.Cat
2 import animal
animal.Cat
3import animal
animal.Cat
4 import animal
animal.Cat
5import animal
animal.Cat
6 import animal
animal.Cat
7import animal
animal.Cat
8import animal
animal.Cat
9Season.SPRING SPRING 1 [, , , ]0
Season.SPRING SPRING 1 [, , , ]1
import animal
animal.Cat
8Season.SPRING SPRING 1 [, , , ]3
Season.SPRING SPRING 1 [, , , ]0
Season.SPRING SPRING 1 [, , , ]5
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER1
operator
6operator
7Season.SPRING SPRING 1 [, , , ]5
operator
9The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER1
Enum.
2Enum.
3Enum.
4Enum.
5Output:
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3
import animal
animal.Cat
8Season.SPRING
SPRING
1
[, , , ]
7Season.SPRING
SPRING
1
[, , , ]
0 Season.SPRING
SPRING
1
[, , , ]
9 iterable. They can be iterated using loops
animal.Cat
import animal
animal.Cat
8The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31
Season.SPRING SPRING 1 [, , , ]0
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33we will use for loop to print all the members of the Enum class.
Python3
Ví dụ 1: Lớp Enum trong Python
Mã python để chứng minh sự liệt kê & nbsp;
import animal
animal.Cat
2 import animal
animal.Cat
3import animal
animal.Cat
4 import animal
animal.Cat
5import animal
animal.Cat
6 import animal
animal.Cat
7import animal
animal.Cat
8import animal
animal.Cat
9Season.SPRING SPRING 1 [, , , ]0
Season.SPRING SPRING 1 [, , , ]1
import animal
animal.Cat
8Season.SPRING SPRING 1 [, , , ]3
Season.SPRING SPRING 1 [, , , ]0
Season.SPRING SPRING 1 [, , , ]5
import animal
animal.Cat
8Season.SPRING SPRING 1 [, , , ]7
Season.SPRING SPRING 1 [, , , ]0
Season.SPRING SPRING 1 [, , , ]9
import animal
animal.Cat
8The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34
import animal
animal.Cat
14import animal
animal.Cat
15import animal
animal.Cat
16Output:
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER
import animal
animal.Cat
8The enum member associated with value 2 is : SUMMER
The enum member associated with name AUTUMN is : 3
1Season.SPRING
SPRING
1
[, , , ]
0 The enum member associated with value 2 is : SUMMER
The enum member associated with name AUTUMN is : 3
3hashing
animal.Cat
Ví dụ 2: Truy cập chế độ & NBSP;
Python3
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.
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ị.
import animal
animal.Cat
6 import animal
animal.Cat
7Ví 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.
import animal
animal.Cat
08 import animal
animal.Cat
09import animal
animal.Cat
10 import animal
animal.Cat
11Ví dụ 4: Hỗ trợ liệt kê băm
import animal
animal.Cat
8The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER1
import animal
animal.Cat
54import animal
animal.Cat
55import animal
animal.Cat
56import animal
animal.Cat
57import animal
animal.Cat
8The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER1
import animal
animal.Cat
61import animal
animal.Cat
55Output:
Enum is hashed
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ộ.Enumerations support two types of comparisons
import animal
4
animal.Catimport animal
18:- These are checked using keywords “is” and “is not“.
animal.Catimport animal
6
animal.Catimport animal
20 :- Equality comparisons of “==” and “!=” types are also supported.
animal.Cat
Python3
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.
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ị.
import animal
animal.Cat
6 import animal
animal.Cat
7Ví 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.
import animal
animal.Cat
56import animal
animal.Cat
57import animal
animal.Cat
8The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER1
import animal
animal.Cat
93import animal
animal.Cat
55import animal
animal.Cat
08 import animal
animal.Cat
09import animal
animal.Cat
10 import animal
animal.Cat
11import animal
animal.Cat
8The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER1
Season.SPRING SPRING 1 [, , , ]02
import animal
animal.Cat
55import animal
animal.Cat
56import animal
animal.Cat
57import animal
animal.Cat
8The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER1
Season.SPRING SPRING 1 [, , , ]09
import animal
animal.Cat
55Output:
Dog and cat are different animals Lions and cat are different