Hướng dẫn java enum equivalent in python - java enum tương đương trong python

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
33
Season.SPRING
SPRING
1


[, , , ]
0
Season.SPRING
SPRING
1


[, , , ]
0
import animal
animal.Cat
46
import animal
animal.Cat
38
import animal
animal.Cat
48
import animal
animal.Cat
41
import animal
animal.Cat
50

Ví 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ó Enum

và 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 https://docs.python.org/3/l Library/enum.html 🙂

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • 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
    3
    import animal
    animal.Cat
    4
    import animal
    animal.Cat
    5

    import animal
    animal.Cat
    6
    import animal
    animal.Cat
    7

    import animal
    animal.Cat
    8
    import animal
    animal.Cat
    9
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    0
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    1

    import animal
    animal.Cat
    8
    Season.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 :  3
    4
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    5

    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
    7

    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
    9

    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    4
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    1
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    2
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    3

    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    4
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    1
    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
    3

    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    4
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    1
    Enum is hashed
    0
    Enum is hashed
    1

    Output: 

    Season.SPRING
    SPRING
    1
    
    
    [, , , ]

    import animalanimal.Cat8Season.SPRING SPRING 1 [, , , ]7Season.SPRING SPRING 1 [, , , ]0 Season.SPRING SPRING 1 [, , , ]9 

    import animal
    animal.Cat
    8
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    1
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    0
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    3

    • 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
    3
    import animal
    animal.Cat
    4
    import animal
    animal.Cat
    5

    import animal
    animal.Cat
    6
    import animal
    animal.Cat
    7

    import animal
    animal.Cat
    8
    import animal
    animal.Cat
    9
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    0
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    1

    import animal
    animal.Cat
    8
    Season.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 :  3
    4
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    1operator6operator7
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    5operator9

    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    4
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    1Enum.2Enum.3Enum.4Enum.5

    Output: 

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

    import animalanimal.Cat8Season.SPRING SPRING 1 [, , , ]7Season.SPRING SPRING 1 [, , , ]0 Season.SPRING SPRING 1 [, , , ]9 iterable. They can be iterated using loops

    import animal
    animal.Cat
    8
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    1
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    0
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    3we 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
    3
    import animal
    animal.Cat
    4
    import animal
    animal.Cat
    5

    import animal
    animal.Cat
    6
    import animal
    animal.Cat
    7

    import animal
    animal.Cat
    8
    import animal
    animal.Cat
    9
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    0
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    1

    import animal
    animal.Cat
    8
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    3
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    0
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    5

    import animal
    animal.Cat
    8
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    7
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    0
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    9

    import animal
    animal.Cat
    8
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    4
    import animal
    animal.Cat
    14
    import animal
    animal.Cat
    15
    import animal
    animal.Cat
    16

    Output: 

    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER

    import animalanimal.Cat8The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31Season.SPRING SPRING 1 [, , , ]0 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33hashing

    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
    7

    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.

    import animal
    animal.Cat
    08
    import animal
    animal.Cat
    09
    import animal
    animal.Cat
    10
    import animal
    animal.Cat
    11

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

    import animal
    animal.Cat
    8
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    4
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    1
    import animal
    animal.Cat
    54
    import animal
    animal.Cat
    55

    import animal
    animal.Cat
    56
    import animal
    animal.Cat
    57

    import animal
    animal.Cat
    8
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    4
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    1
    import animal
    animal.Cat
    61
    import animal
    animal.Cat
    55

    Output:

    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
      animal.Cat
      4
      import animal
      animal.Cat
      18
      :- These are checked using keywords “is” and “is not“.
    • import animal
      animal.Cat
      6
      import animal
      animal.Cat
      20
      :- Equality comparisons of “==” and “!=” types are also supported.

    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
    7

    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.

    import animal
    animal.Cat
    56
    import animal
    animal.Cat
    57

    import animal
    animal.Cat
    8
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    4
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    1
    import animal
    animal.Cat
    93
    import animal
    animal.Cat
    55

    import animal
    animal.Cat
    08
    import animal
    animal.Cat
    09
    import animal
    animal.Cat
    10
    import animal
    animal.Cat
    11

    import animal
    animal.Cat
    8
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    4
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    1
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    02
    import animal
    animal.Cat
    55

    import animal
    animal.Cat
    56
    import animal
    animal.Cat
    57

    import animal
    animal.Cat
    8
    The enum member associated with value 2 is :  SUMMER
    The enum member associated with name AUTUMN is :  3
    4
    1 - Season.SPRING
    2 - Season.SUMMER
    3 - Season.AUTUMN
    4 - Season.WINTER
    1
    Season.SPRING
    SPRING
    1
    
    
    [, , , ]
    09
    import animal
    animal.Cat
    55

    Output: 

    Dog and cat are different animals
    Lions and cat are different

    Tương đương với enum trong Python là gì?

    Các enum là biểu diễn chuỗi có thể đánh giá của một đối tượng còn được gọi là repr ().repr().

    Tôi có nên sử dụng Enum trong Python không?

    Các enum Python rất hữu ích để đại diện cho dữ liệu đại diện cho một tập hợp các trạng thái hữu hạn như ngày trong tuần, tháng trong năm, v.v. Chúng đã được thêm vào Python 3.4 qua PEP 435. Tuy nhiên, nó có sẵn tất cả các cách trở lại 2.4 thông quapypy.Như vậy, bạn có thể mong đợi chúng là một yếu tố chính khi bạn khám phá lập trình Python. such as days of the week, months of the year, etc. They were added to Python 3.4 via PEP 435. However, it is available all the way back to 2.4 via pypy. As such, you can expect them to be a staple as you explore Python programming.

    Java có thể so sánh được không?

    Các hằng số enum chỉ có thể so sánh với các hằng số enum khác cùng loại enum.Thứ tự tự nhiên được thực hiện theo phương pháp này là thứ tự mà các hằng số được khai báo.Tham số: O - đối tượng được so sánh.. The natural order implemented by this method is the order in which the constants are declared. Parameters: o - the object to be compared.

    Enum và Enumerator có giống nhau không?

    Trong lập trình máy tính, một loại được liệt kê (còn được gọi là liệt kê, enum hoặc yếu tố trong ngôn ngữ lập trình R và biến phân loại trong thống kê) là một loại dữ liệu bao gồm một tập hợp các giá trị được đặt tên được gọi là các phần tử, thành viên, người lập chương hoặc điều tra của các trình điều khiển củaLoại.