Hướng dẫn install enum python - cài đặt enum python

Các enum đã được thêm vào Python 3,4 như được mô tả trong PEP 435. Nó cũng đã được đặt lại thành 3,3, 3.2, 3.1, 2.7, 2.6, 2.5 và 2.4 trên PYPI.

Show

Đối với các kỹ thuật enum nâng cao hơn, hãy thử thư viện AENUM (2.7, 3.3+, cùng tác giả với

>>> Numbers.reverse_mapping['three']
'THREE'
3. Mã không hoàn toàn tương thích giữa PY2 và PY3, ví dụ: bạn sẽ cần
>>> Numbers.reverse_mapping['three']
'THREE'
4 trong Python 2).

  • Để sử dụng
    >>> Numbers.reverse_mapping['three']
    'THREE'
    
    3, làm
    >>> Numbers.reverse_mapping['three']
    'THREE'
    
    6
  • Để sử dụng
    >>> Numbers.reverse_mapping['three']
    'THREE'
    
    7, làm
    >>> Numbers.reverse_mapping['three']
    'THREE'
    
    8

Cài đặt

>>> Numbers.reverse_mapping['three']
'THREE'
9 (không có số) sẽ cài đặt phiên bản hoàn toàn khác và không tương thích.


from enum import Enum     # for enum34, or the stdlib version
# from aenum import Enum  # for the aenum version
Animal = Enum('Animal', 'ant bee cat dog')

Animal.ant  # returns 
Animal['ant']  # returns  (string lookup)
Animal.ant.name  # returns 'ant' (inverse lookup)

hoặc tương đương:

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4

Trong các phiên bản trước, một cách hoàn thành enums là:

def enum(**enums):
    return type('Enum', (), enums)

được sử dụng như vậy:

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'

Bạn cũng có thể dễ dàng hỗ trợ liệt kê tự động với một cái gì đó như thế này:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)

và được sử dụng như vậy:

>>> Numbers = enum('ZERO', 'ONE', 'TWO')
>>> Numbers.ZERO
0
>>> Numbers.ONE
1

Hỗ trợ chuyển đổi các giá trị trở lại tên có thể được thêm vào theo cách này:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)

Điều này ghi đè bất cứ điều gì với tên đó, nhưng nó rất hữu ích cho việc hiển thị các enum của bạn trong đầu ra. Nó sẽ ném

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

0 nếu ánh xạ ngược không tồn tại. Với ví dụ đầu tiên:

>>> Numbers.reverse_mapping['three']
'THREE'

Nếu bạn đang sử dụng MyPy một cách khác để thể hiện "Enums" là với

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

1.

Ví dụ:

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

_Private__names¶

Tên riêng sẽ là các thuộc tính bình thường trong Python 3.11 thay vì lỗi hoặc thành viên (tùy thuộc vào việc tên kết thúc bằng dấu gạch dưới). Sử dụng các tên này trong 3.10 sẽ phát hành

def enum(**enums):
    return type('Enum', (), enums)
96. Lib/enum.py


from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 Kiểu thành viên

Ghi chú

Trong mã Python 2, thuộc tính

def enum(**enums):
    return type('Enum', (), enums)
87 là cần thiết vì thứ tự định nghĩa bị mất trước khi có thể ghi lại.

_Private__names¶

Tên riêng sẽ là các thuộc tính bình thường trong Python 3.11 thay vì lỗi hoặc thành viên (tùy thuộc vào việc tên kết thúc bằng dấu gạch dưới). Sử dụng các tên này trong 3.10 sẽ phát hành def enum(**enums): return type('Enum', (), enums) 96.

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 Kiểu thành viên

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 Thành viên là trường hợp của lớp
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 của họ và thường được truy cập là
>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
00. Trong một số trường hợp nhất định, chúng cũng có thể được truy cập là
>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
01, nhưng bạn không bao giờ nên làm điều này vì tra cứu đó có thể thất bại hoặc tệ hơn, trả lại một cái gì đó bên cạnh thành viên
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 mà bạn đang tìm ):

Hành vi này không được chấp nhận và sẽ bị xóa trong 3.11.

Thay đổi trong phiên bản 3.5.

Giá trị boolean của các lớp

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 và thành viên

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 Các thành viên được trộn với các loại không phải là -____ 82 (chẳng hạn như
>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
2,
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
11, v.v.) được đánh giá theo các quy tắc loại hỗn hợp; Mặt khác, tất cả các thành viên đánh giá là
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
59. Để làm cho đánh giá boolean của riêng bạn phụ thuộc vào giá trị thành viên, cộng thêm giá trị sau vào lớp của bạn:

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 Các lớp luôn đánh giá là
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
59.

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 Các lớp với Phương thức lor

Nếu bạn cung cấp các phương thức phụ

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 của bạn, như lớp Planet ở trên, các phương thức đó sẽ hiển thị trong một
>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
13 của thành viên, nhưng không phải của lớp:

Kết hợp các thành viên của ________ 84¶()

Nếu một kết hợp các thành viên cờ không được đặt tên,

def enum(**enums):
    return type('Enum', (), enums)
40 sẽ bao gồm tất cả các cờ được đặt tên và tất cả các kết hợp các cờ có tên trong giá trị:

Trong 3.11, các kết hợp cờ không được đặt tên sẽ chỉ tạo ra các thành viên cờ chính tắc (còn gọi là cờ có giá trị đơn). Vì vậy,
>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
16 sẽ tạo ra một cái gì đó như
>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
17.

Mới trong phiên bản 3.4.

Mã nguồn: lib/enum.py

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

4,
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5,
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

7

Một liệt kê là một tập hợp các tên biểu tượng (thành viên) bị ràng buộc với các giá trị duy nhất, không đổi. Trong một bảng liệt kê, các thành viên có thể được so sánh bằng danh tính và bản thân việc liệt kê có thể được lặp lại.

Trường hợp của các thành viên enum

>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...

Ghi chú

Giá trị thành viên enum

Giá trị thành viên có thể là bất cứ điều gì:

>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
2,
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
11, v.v. Phải cẩn thận nếu bạn trộn
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

7 với các giá trị khác.

Ghi chú

Danh pháp

  • Lớp

    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    14 là một liệt kê (hoặc enum)

  • Các thuộc tính

    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    15,
    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    16, v.v., là các thành viên liệt kê (hoặc thành viên enum) và là hằng số chức năng.

  • Các thành viên ENUM có tên và giá trị (tên của

    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    15 là
    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    18, giá trị của
    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    19 là
    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    20, v.v.)

Các thành viên liệt kê có các biểu diễn chuỗi có thể đọc được của con người:

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
0

Trong khi

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
21 của họ có thêm thông tin:

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
1

Loại thành viên liệt kê là bảng liệt kê mà nó thuộc về:

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
2

Các thành viên của Enum cũng có một tài sản chỉ chứa tên mặt hàng của họ:

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
3

Tái liệt trình hỗ trợ lặp lại, theo thứ tự định nghĩa:

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
4

Các thành viên liệt kê có thể băm, vì vậy chúng có thể được sử dụng trong từ điển và bộ:

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
5

Truy cập chương trình vào các thành viên bảng liệt kê và các thuộc tính của họ Jo

Đôi khi, nó rất hữu ích để truy cập các thành viên trong các lập trình theo chương trình (nghĩa là các tình huống trong đó

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
15 won đã làm vì màu chính xác không được biết đến trong thời gian viết chương trình).
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 cho phép truy cập như vậy:

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
6

Nếu bạn muốn truy cập các thành viên Enum theo tên, hãy sử dụng quyền truy cập mục:

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
7

Nếu bạn có thành viên Enum và cần

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
24 hoặc
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
25:

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
8

Sao chép các thành viên và giá trị enum

Có hai thành viên enum có cùng tên là không hợp lệ:

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
9

Tuy nhiên, hai thành viên ENUM được phép có cùng giá trị. Cho hai thành viên A và B có cùng giá trị (và đầu tiên được xác định), B là bí danh đối với A. Tra cứu giá trị của giá trị của A và B sẽ trả về A. Tra cứu tên B của B cũng sẽ trả về A:

def enum(**enums):
    return type('Enum', (), enums)
0

Ghi chú

Cố gắng tạo một thành viên có cùng tên với thuộc tính đã được xác định (một thành viên khác, một phương thức, v.v.) hoặc cố gắng tạo một thuộc tính có cùng tên với thành viên không được phép.

Đảm bảo các giá trị liệt kê duy nhất

Theo mặc định, việc liệt kê cho phép nhiều tên là bí danh cho cùng một giá trị. Khi hành vi này không được mong muốn, bộ trang trí sau đây có thể được sử dụng để đảm bảo mỗi giá trị chỉ được sử dụng một lần trong bảng liệt kê:

________ 126 ________ 88 ________ 102¶

Một nhà trang trí

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
08 dành riêng cho các liệt kê. Nó tìm kiếm một bảng liệt kê từ
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
30 thu thập bất kỳ bí danh nào mà nó tìm thấy; Nếu bất kỳ tìm thấy
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
31 được nêu ra với các chi tiết:

def enum(**enums):
    return type('Enum', (), enums)
1

Sử dụng các giá trị tự động

Nếu giá trị chính xác là không quan trọng, bạn có thể sử dụng

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

7:

def enum(**enums):
    return type('Enum', (), enums)
2

Các giá trị được chọn bởi

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
33, có thể bị ghi đè:

def enum(**enums):
    return type('Enum', (), enums)
3

Ghi chú

Mục tiêu của phương pháp

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
33 mặc định là cung cấp trình tự
>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
2 tiếp theo với trình tự
>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
2 cuối cùng được cung cấp, nhưng cách nó thực hiện đây là một chi tiết triển khai và có thể thay đổi.

Ghi chú

Phương pháp

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
33 phải được xác định trước bất kỳ thành viên nào.

Sự lặp lại

Lặp lại các thành viên của một enum không cung cấp các bí danh:

def enum(**enums):
    return type('Enum', (), enums)
4

Thuộc tính đặc biệt

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
30 là một bản đồ tên chỉ được đọc cho các thành viên. Nó bao gồm tất cả các tên được xác định trong bảng liệt kê, bao gồm các bí danh:

def enum(**enums):
    return type('Enum', (), enums)
5

Thuộc tính

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
30 có thể được sử dụng để truy cập chương trình chi tiết cho các thành viên liệt kê. Ví dụ: tìm tất cả các bí danh:

def enum(**enums):
    return type('Enum', (), enums)
6

So sánh lor

Các thành viên liệt kê được so sánh bằng danh tính:

def enum(**enums):
    return type('Enum', (), enums)
7

So sánh được đặt hàng giữa các giá trị liệt kê không được hỗ trợ. Các thành viên enum không phải là số nguyên (nhưng xem intenum bên dưới):

def enum(**enums):
    return type('Enum', (), enums)
8

So sánh bình đẳng được xác định mặc dù:

def enum(**enums):
    return type('Enum', (), enums)
9

So sánh với các giá trị không kích thích sẽ luôn so sánh không bằng nhau (một lần nữa,

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

3 được thiết kế rõ ràng để hoạt động khác nhau, xem bên dưới):

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
0

Các thành viên được phép và các thuộc tính của bảng liệt kê Jo

Các ví dụ trên sử dụng số nguyên cho các giá trị liệt kê. Sử dụng số nguyên ngắn và tiện dụng (và được cung cấp theo mặc định bởi API chức năng), nhưng không được thực thi nghiêm ngặt. Trong phần lớn các trường hợp sử dụng, người ta không quan tâm đến giá trị thực tế của một liệt kê là gì. Nhưng nếu giá trị là quan trọng, bảng liệt kê có thể có các giá trị tùy ý.

Các bảng liệt kê là các lớp Python, và có thể có các phương pháp và phương pháp đặc biệt như bình thường. Nếu chúng ta có sự liệt kê này:

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
1

Then:

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
2

Các quy tắc cho những gì được cho phép như sau: các tên bắt đầu và kết thúc với một dấu gạch dưới được bảo lưu bởi enum và không thể được sử dụng; Tất cả các thuộc tính khác được xác định trong một bảng liệt kê sẽ trở thành thành viên của liệt kê này, ngoại trừ các phương pháp đặc biệt (

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
41,
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
42, v.v.), các mô tả (Phương pháp cũng là mô tả) và tên biến được liệt kê trong
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
43.

Lưu ý: Nếu liệt kê của bạn định nghĩa

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
44 và/hoặc
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
45 thì bất kỳ giá trị nào được trao cho thành viên ENUM sẽ được chuyển vào các phương thức đó. Xem hành tinh để biết một ví dụ.

Các lớp con enum bị hạn chế

Một lớp

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 mới phải có một lớp enum cơ sở, tối đa một loại dữ liệu cụ thể và nhiều lớp mixin dựa trên ____ 147 khi cần thiết. Thứ tự của các lớp cơ sở này là:

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
3

Ngoài ra, phân lớp một liệt kê chỉ được phép nếu việc liệt kê không xác định bất kỳ thành viên nào. Vì vậy, điều này bị cấm:

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
4

Nhưng điều này được cho phép:

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
5

Cho phép phân lớp các enum xác định các thành viên sẽ dẫn đến vi phạm một số bất biến quan trọng của các loại và trường hợp. Mặt khác, thật hợp lý khi cho phép chia sẻ một số hành vi phổ biến giữa một nhóm các liệt kê. (Xem đặt hàng cho một ví dụ.)

Nước ngâm

Tái lập có thể được ngâm và không được giải thích:

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
6

Các hạn chế thông thường đối với Pickling áp dụng: Các enum có thể chọn phải được xác định ở cấp cao nhất của một mô -đun, vì việc không bị ràng buộc đòi hỏi chúng phải được nhập từ mô -đun đó.

Ghi chú

Với giao thức Pickle Phiên bản 4, có thể dễ dàng pickle enum lồng nhau trong các lớp khác.

Có thể sửa đổi cách các thành viên enum được ngâm/không được giải thích bằng cách xác định

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
48 trong lớp liệt kê.

API chức năng

Lớp

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 có thể gọi được, cung cấp API chức năng sau:

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
7

Các ngữ nghĩa của API này giống với

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
50. Đối số đầu tiên của cuộc gọi đến
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 là tên của bảng liệt kê.

Đối số thứ hai là nguồn gốc của tên thành viên liệt kê. Nó có thể là một chuỗi các tên được phân tách bằng khoảng trắng, một chuỗi các tên, một chuỗi các bộ 2 với các cặp khóa/giá trị hoặc ánh xạ (ví dụ: từ điển) của các tên thành các giá trị. Hai tùy chọn cuối cùng cho phép gán các giá trị tùy ý cho các bảng điều khiển; Các số nguyên tăng tự động khác bắt đầu với 1 (sử dụng tham số

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
52 để chỉ định giá trị bắt đầu khác). Một lớp mới có nguồn gốc từ
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 được trả về. Nói cách khác, nhiệm vụ trên cho
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
54 tương đương với:

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
8

Lý do mặc định đến

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
55 là số khởi đầu chứ không phải
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
56 là
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
56 là
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
58 theo nghĩa boolean, nhưng tất cả các thành viên enum đều đánh giá thành
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
59.

Các enum pickling được tạo bằng API chức năng có thể khó khăn vì các chi tiết triển khai ngăn xếp khung được sử dụng để thử và tìm ra mô -đun nào mà liệt kê được tạo trong (ví dụ: nó sẽ thất bại nếu bạn sử dụng chức năng tiện ích trong mô -đun riêng biệt và cũng có thể không hoạt động trên Ironpython hoặc Jython). Giải pháp là chỉ định tên mô -đun một cách rõ ràng như sau:

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
9

Cảnh báo

Nếu

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
60 không được cung cấp và enum không thể xác định nó là gì, các thành viên enum mới sẽ không thể không được biết đến; Để giữ lỗi gần hơn với nguồn, Pickling sẽ bị tắt.

Trong một số trường hợp, giao thức Pickle 4 mới, dựa vào

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
61 được đặt đến vị trí nơi Pickle sẽ có thể tìm thấy lớp. Ví dụ: nếu lớp học được cung cấp trong lớp một ngày nào đó trong phạm vi toàn cầu:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)
0

Chữ ký hoàn chỉnh là:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)
1

giá trị

Những gì lớp enum mới sẽ ghi lại như tên của nó.

Tên

Các thành viên enum. Đây có thể là một chuỗi khoảng trắng hoặc dấu phẩy (giá trị sẽ bắt đầu ở mức 1 trừ khi có quy định khác):

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)
2

hoặc một người lặp của tên:

hoặc một trình lặp của các cặp (tên, giá trị):

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)
3

hoặc một bản đồ:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)
4

Mô -đun

Tên của mô -đun nơi có thể tìm thấy lớp Enum mới.

tên gọi

nơi có thể tìm thấy lớp enum mới.

loại hình

Nhập để trộn vào lớp Enum mới.

bắt đầu

Số để bắt đầu đếm tại nếu chỉ có tên được truyền vào.

Thay đổi trong phiên bản 3.5: Tham số bắt đầu đã được thêm vào.The start parameter was added.

Có nguồn gốc từ

Intenum¶

Biến thể đầu tiên của

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 được cung cấp cũng là một lớp con của
>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
2. Các thành viên của
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

3 có thể được so sánh với các số nguyên; Bằng cách mở rộng, các loại thuốc của các loại khác nhau cũng có thể được so sánh với nhau:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)
5

Tuy nhiên, họ vẫn có thể được so sánh với các liệt kê

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 tiêu chuẩn:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)
6

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

3 Các giá trị hoạt động như số nguyên theo những cách khác mà bạn mong đợi:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)
7

Intflag¶

Biến thể tiếp theo của

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 được cung cấp,
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5, cũng dựa trên
>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
2. Sự khác biệt là
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5 Các thành viên có thể được kết hợp bằng cách sử dụng các toán tử bitwise (&, |, ^, ~) và kết quả vẫn là thành viên
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5. Tuy nhiên, như tên gọi,
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5 Các thành viên cũng phân lớp
>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
2 và có thể được sử dụng bất cứ nơi nào sử dụng
>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
2. Bất kỳ hoạt động nào trên một thành viên
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5 bên cạnh các hoạt động bit khôn ngoan sẽ mất tư cách thành viên
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5.

Mới trong phiên bản 3.6.

Mẫu

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5 Lớp:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)
8

Cũng có thể đặt tên cho các kết hợp:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)
9

Một sự khác biệt quan trọng khác giữa

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5 và
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 là nếu không có cờ được đặt (giá trị là 0), đánh giá boolean của nó là
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
58:

>>> Numbers = enum('ZERO', 'ONE', 'TWO')
>>> Numbers.ZERO
0
>>> Numbers.ONE
1
0

Bởi vì các thành viên

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5 cũng là các lớp con của
>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
2, họ có thể được kết hợp với họ:

>>> Numbers = enum('ZERO', 'ONE', 'TWO')
>>> Numbers.ZERO
0
>>> Numbers.ONE
1
1

Lá cờ¶

Biến thể cuối cùng là

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

4. Giống như
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5,
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

4 Thành viên có thể được kết hợp bằng cách sử dụng các toán tử bitwise (&, |, ^, ~). Không giống như
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5, chúng không thể được kết hợp với, cũng như không so sánh với bất kỳ liệt kê
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

4 nào khác, cũng như
>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
2. Mặc dù có thể chỉ định các giá trị trực tiếp nhưng nó được khuyến nghị sử dụng
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

7 làm giá trị và để
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

4 chọn một giá trị thích hợp.

Mới trong phiên bản 3.6.

Giống như

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5, nếu sự kết hợp của
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

4 Các thành viên dẫn đến không có cờ nào được thiết lập, thì đánh giá Boolean là
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
58:

>>> Numbers = enum('ZERO', 'ONE', 'TWO')
>>> Numbers.ZERO
0
>>> Numbers.ONE
1
2

Cờ riêng lẻ nên có các giá trị là sức mạnh của hai (1, 2, 4, 8, phạm), trong khi các kết hợp cờ giành được:

>>> Numbers = enum('ZERO', 'ONE', 'TWO')
>>> Numbers.ZERO
0
>>> Numbers.ONE
1
3

Đặt tên cho điều kiện của No No Flags Set không thay đổi giá trị boolean của nó:

>>> Numbers = enum('ZERO', 'ONE', 'TWO')
>>> Numbers.ZERO
0
>>> Numbers.ONE
1
4

Ghi chú

Đối với phần lớn mã mới,

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 và
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

4 được khuyến nghị mạnh mẽ, vì
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

3 và
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5 phá vỡ một số lời hứa ngữ nghĩa về một liệt kê (bằng cách so sánh với các số nguyên, và do đó bằng cách chuyển tiếp đến các bảng điều khiển không liên quan khác).
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

3 và
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5 chỉ nên được sử dụng trong trường hợp
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 và
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

4 sẽ không làm; Ví dụ, khi các hằng số nguyên được thay thế bằng các liệt kê hoặc cho khả năng tương tác với các hệ thống khác.

Khác¶

Mặc dù

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

3 là một phần của mô -đun
def enum(**enums):
    return type('Enum', (), enums)
03, nhưng sẽ rất đơn giản để thực hiện độc lập:

>>> Numbers = enum('ZERO', 'ONE', 'TWO')
>>> Numbers.ZERO
0
>>> Numbers.ONE
1
5

Điều này cho thấy làm thế nào các liệt kê có nguồn gốc tương tự có thể được xác định; Ví dụ,

def enum(**enums):
    return type('Enum', (), enums)
04 trộn trong
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
11 thay vì
>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
2.

Một số quy tắc:

  1. Khi phân lớp

    from typing import Literal #python >=3.8
    from typing_extensions import Literal #python 2.7, 3.4-3.7
    
    
    Animal = Literal['ant', 'bee', 'cat', 'dog']
    
    def hello_animal(animal: Animal):
        print(f"hello {animal}")
    
    hello_animal('rock') # error
    hello_animal('bee') # passes
    
    
    2, các loại hỗn hợp phải xuất hiện trước chính
    from typing import Literal #python >=3.8
    from typing_extensions import Literal #python 2.7, 3.4-3.7
    
    
    Animal = Literal['ant', 'bee', 'cat', 'dog']
    
    def hello_animal(animal: Animal):
        print(f"hello {animal}")
    
    hello_animal('rock') # error
    hello_animal('bee') # passes
    
    
    2 trong chuỗi các cơ sở, như trong ví dụ
    from typing import Literal #python >=3.8
    from typing_extensions import Literal #python 2.7, 3.4-3.7
    
    
    Animal = Literal['ant', 'bee', 'cat', 'dog']
    
    def hello_animal(animal: Animal):
        print(f"hello {animal}")
    
    hello_animal('rock') # error
    hello_animal('bee') # passes
    
    
    3 ở trên.

  2. Mặc dù

    from typing import Literal #python >=3.8
    from typing_extensions import Literal #python 2.7, 3.4-3.7
    
    
    Animal = Literal['ant', 'bee', 'cat', 'dog']
    
    def hello_animal(animal: Animal):
        print(f"hello {animal}")
    
    hello_animal('rock') # error
    hello_animal('bee') # passes
    
    
    2 có thể có các thành viên thuộc bất kỳ loại nào, một khi bạn trộn vào một loại bổ sung, tất cả các thành viên phải có giá trị của loại đó, ví dụ:
    >>> from enum import Enum
    >>> class Color(Enum):
    ...     RED = 1
    ...     GREEN = 2
    ...     BLUE = 3
    ...
    
    2 ở trên. Hạn chế này không áp dụng cho hỗn hợp mà chỉ thêm các phương thức và don chỉ định một loại khác.

  3. Khi một loại dữ liệu khác được trộn lẫn, thuộc tính

    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    25 không giống với chính thành viên Enum, mặc dù nó tương đương và sẽ so sánh bằng nhau.

  4. Định dạng theo kiểu %: %s và %r gọi lớp

    from typing import Literal #python >=3.8
    from typing_extensions import Literal #python 2.7, 3.4-3.7
    
    
    Animal = Literal['ant', 'bee', 'cat', 'dog']
    
    def hello_animal(animal: Animal):
        print(f"hello {animal}")
    
    hello_animal('rock') # error
    hello_animal('bee') # passes
    
    
    2 lần lượt là
    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    41 và
    def enum(**enums):
        return type('Enum', (), enums)
    
    15; Các mã khác (chẳng hạn như %I hoặc %H cho intenum) coi thành viên enum là loại hỗn hợp của nó.

  5. Các chuỗi chữ được định dạng,

    def enum(**enums):
        return type('Enum', (), enums)
    
    16 và
    def enum(**enums):
        return type('Enum', (), enums)
    
    17 sẽ sử dụng loại hỗn hợp loại ____ ____218 trừ khi
    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    41 hoặc
    def enum(**enums):
        return type('Enum', (), enums)
    
    18 được ghi đè trong lớp con, trong trường hợp đó, các phương thức được ghi đè hoặc
    from typing import Literal #python >=3.8
    from typing_extensions import Literal #python 2.7, 3.4-3.7
    
    
    Animal = Literal['ant', 'bee', 'cat', 'dog']
    
    def hello_animal(animal: Animal):
        print(f"hello {animal}")
    
    hello_animal('rock') # error
    hello_animal('bee') # passes
    
    
    2 sẽ được sử dụng. Sử dụng mã định dạng! S và!,
    def enum(**enums):
        return type('Enum', (), enums)
    
    16, and
    def enum(**enums):
        return type('Enum', (), enums)
    
    17 will use the mixed-in type’s
    def enum(**enums):
        return type('Enum', (), enums)
    
    18 unless
    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    41 or
    def enum(**enums):
        return type('Enum', (), enums)
    
    18 is overridden in the subclass, in which case the overridden methods or
    from typing import Literal #python >=3.8
    from typing_extensions import Literal #python 2.7, 3.4-3.7
    
    
    Animal = Literal['ant', 'bee', 'cat', 'dog']
    
    def hello_animal(animal: Animal):
        print(f"hello {animal}")
    
    hello_animal('rock') # error
    hello_animal('bee') # passes
    
    
    2 methods will be used. Use the !s and !r format codes to force usage of the
    from typing import Literal #python >=3.8
    from typing_extensions import Literal #python 2.7, 3.4-3.7
    
    
    Animal = Literal['ant', 'bee', 'cat', 'dog']
    
    def hello_animal(animal: Animal):
        print(f"hello {animal}")
    
    hello_animal('rock') # error
    hello_animal('bee') # passes
    
    
    2 class’s
    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    41 and
    def enum(**enums):
        return type('Enum', (), enums)
    
    15 methods.

Khi nào nên sử dụng class Animal(Enum): ant = 1 bee = 2 cat = 3 dog = 4 44 so với ________ 145¶

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
44 phải được sử dụng bất cứ khi nào bạn muốn tùy chỉnh giá trị thực của thành viên
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2. Bất kỳ sửa đổi nào khác có thể đi trong
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
44 hoặc
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
45, với
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
45 được ưu tiên.

Ví dụ: nếu bạn muốn chuyển một số mục cho hàm tạo, nhưng chỉ muốn một trong số chúng là giá trị:

>>> Numbers = enum('ZERO', 'ONE', 'TWO')
>>> Numbers.ZERO
0
>>> Numbers.ONE
1
6

Ví dụ thú vị

Trong khi

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2,
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

3,
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

5 và
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

4 dự kiến ​​sẽ bao gồm phần lớn các trường hợp sử dụng, họ không thể bao gồm tất cả. Dưới đây là các công thức nấu ăn cho một số loại bảng liệt kê khác nhau có thể được sử dụng trực tiếp hoặc làm ví dụ để tạo một bản thân riêng.

Bỏ qua các giá trị

Trong nhiều trường hợp sử dụng, người ta không quan tâm đến giá trị thực tế của một bảng liệt kê là gì. Có một số cách để xác định loại liệt kê đơn giản này:

  • Sử dụng các trường hợp

    from typing import Literal #python >=3.8
    from typing_extensions import Literal #python 2.7, 3.4-3.7
    
    
    Animal = Literal['ant', 'bee', 'cat', 'dog']
    
    def hello_animal(animal: Animal):
        print(f"hello {animal}")
    
    hello_animal('rock') # error
    hello_animal('bee') # passes
    
    
    7 cho giá trị

  • Sử dụng các trường hợp

    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    47 làm giá trị

  • Sử dụng chuỗi mô tả làm giá trị

  • Sử dụng một tuple làm giá trị và tùy chỉnh

    class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    44 để thay thế bộ tuple bằng giá trị

Sử dụng bất kỳ phương pháp nào trong số này biểu thị cho người dùng rằng các giá trị này không quan trọng và cũng cho phép người ta thêm, xóa hoặc sắp xếp lại các thành viên mà không phải đánh số lại các thành viên còn lại.

Bất cứ phương pháp nào bạn chọn, bạn nên cung cấp một

def enum(**enums):
    return type('Enum', (), enums)
40 cũng che giấu giá trị (không quan trọng):

>>> Numbers = enum('ZERO', 'ONE', 'TWO')
>>> Numbers.ZERO
0
>>> Numbers.ONE
1
7

Sử dụng ________ 87¶

Sử dụng

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

7 sẽ giống như:

>>> Numbers = enum('ZERO', 'ONE', 'TWO')
>>> Numbers.ZERO
0
>>> Numbers.ONE
1
8

Sử dụng ________ 147¶

Sử dụng

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
47 sẽ giống như:

>>> Numbers = enum('ZERO', 'ONE', 'TWO')
>>> Numbers.ZERO
0
>>> Numbers.ONE
1
9

Sử dụng chuỗi mô tả

Sử dụng một chuỗi như giá trị sẽ trông như thế nào:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)
0

Sử dụng tùy chỉnh ____ 144¶

Sử dụng tự động đánh số

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
44 sẽ giống như:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)
1

Để tạo mục đích chung hơn

def enum(**enums):
    return type('Enum', (), enums)
47, hãy thêm
def enum(**enums):
    return type('Enum', (), enums)
48 vào chữ ký:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)
2

Sau đó, khi bạn kế thừa từ

def enum(**enums):
    return type('Enum', (), enums)
47, bạn có thể viết
def enum(**enums):
    return type('Enum', (), enums)
50 của riêng mình để xử lý bất kỳ đối số bổ sung nào:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)
3

Ghi chú

Phương pháp

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
44, nếu được xác định, được sử dụng trong quá trình tạo ra các thành viên ENUM; Sau đó, nó được thay thế bằng Enum từ
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
44 được sử dụng sau khi tạo lớp để tra cứu các thành viên hiện tại.

Đặt hàng

Một liệt kê được đặt hàng không dựa trên

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

3 và do đó duy trì các bất biến
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 bình thường (chẳng hạn như không thể so sánh với các bảng điều khiển khác):

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)
4

Trùng lặpfreeenum¶

Bị lỗi nếu tìm thấy tên thành viên trùng lặp thay vì tạo bí danh:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)
5

Ghi chú

Phương pháp

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
44, nếu được xác định, được sử dụng trong quá trình tạo ra các thành viên ENUM; Sau đó, nó được thay thế bằng Enum từ
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
44 được sử dụng sau khi tạo lớp để tra cứu các thành viên hiện tại.

Đặt hàng

Một liệt kê được đặt hàng không dựa trên

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

3 và do đó duy trì các bất biến
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 bình thường (chẳng hạn như không thể so sánh với các bảng điều khiển khác):

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)
6

Trùng lặpfreeenum¶

Bị lỗi nếu tìm thấy tên thành viên trùng lặp thay vì tạo bí danh:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)
7

Đây là một ví dụ hữu ích để phân lớp enum để thêm hoặc thay đổi các hành vi khác cũng như không cho phép bí danh. Nếu sự thay đổi mong muốn duy nhất là không cho phép bí danh, người trang trí from typing import Literal #python >=3.8 from typing_extensions import Literal #python 2.7, 3.4-3.7 Animal = Literal['ant', 'bee', 'cat', 'dog'] def hello_animal(animal: Animal): print(f"hello {animal}") hello_animal('rock') # error hello_animal('bee') # passes 6 có thể được sử dụng thay thế.

Hành tinh¶

Nếu class Animal(Enum): ant = 1 bee = 2 cat = 3 dog = 4 44 hoặc class Animal(Enum): ant = 1 bee = 2 cat = 3 dog = 4 45 được xác định, giá trị của thành viên enum sẽ được chuyển sang các phương pháp đó:

Thời gian

Một ví dụ để hiển thị thuộc tính class Animal(Enum): ant = 1 bee = 2 cat = 3 dog = 4 43 đang sử dụng:

Enums khác nhau như thế nào? ¶

Enums có một metaclass tùy chỉnh ảnh hưởng đến nhiều khía cạnh của cả hai lớp Enum có nguồn gốc và các trường hợp (thành viên) của chúng.

Các lớp học Enum

Metaclass

def enum(**enums):
    return type('Enum', (), enums)
59 chịu trách nhiệm cung cấp
def enum(**enums):
    return type('Enum', (), enums)
60,
def enum(**enums):
    return type('Enum', (), enums)
61,
def enum(**enums):
    return type('Enum', (), enums)
62 và các phương pháp khác cho phép người ta làm mọi việc với lớp
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 không thành công trên một lớp điển hình, chẳng hạn như danh sách (màu) hoặc some_enum_var có màu.
def enum(**enums):
    return type('Enum', (), enums)
59 chịu trách nhiệm đảm bảo rằng nhiều phương pháp khác trong lớp
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 cuối cùng là chính xác (chẳng hạn như
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
44,
def enum(**enums):
    return type('Enum', (), enums)
67,
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
41 và
def enum(**enums):
    return type('Enum', (), enums)
15).

Các thành viên enum (còn gọi là các trường hợp) ¶

Điều thú vị nhất về các thành viên Enum là họ là những người độc thân.
def enum(**enums):
    return type('Enum', (), enums)
59 tạo ra tất cả trong khi nó đang tạo ra lớp
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2, và sau đó đặt một
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
44 tùy chỉnh để đảm bảo rằng không có trường nào mới được khởi tạo bằng cách chỉ trả lại các trường hợp thành viên hiện có.

  • Tốt hơn điểm¶

  • Được hỗ trợ

    def enum(**enums):
        return type('Enum', (), enums)
    
    73 Tên

  • class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    30 là một bản đồ chỉ được đặt hàng của ________ 275: ________ 276 mục. Nó chỉ có sẵn trên lớp.

  • class Animal(Enum):
        ant = 1
        bee = 2
        cat = 3
        dog = 4
    
    44, nếu được chỉ định, phải tạo và trả lại các thành viên Enum; Đó cũng là một ý tưởng rất tốt để thiết lập thành viên
    def enum(**enums):
        return type('Enum', (), enums)
    
    78 một cách thích hợp. Một khi tất cả các thành viên được tạo ra, nó không còn được sử dụng.

  • Được hỗ trợ

    def enum(**enums):
        return type('Enum', (), enums)
    
    79 Tên

  • def enum(**enums):
        return type('Enum', (), enums)
    
    80 - Tên của thành viên

def enum(**enums):
    return type('Enum', (), enums)
78 - giá trị của thành viên; có thể được đặt / sửa đổi trong
def enum(**enums):
    return type('Enum', (), enums)
82
def enum(**enums):
    return type('Enum', (), enums)
83,
def enum(**enums):
    return type('Enum', (), enums)
87,
def enum(**enums):
    return type('Enum', (), enums)
88

def enum(**enums):
    return type('Enum', (), enums)
83 - Hàm Tra cứu được sử dụng khi không tìm thấy giá trị; có thể bị ghi đè
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
43

class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
43 - một danh sách các tên, dưới dạng
def enum(**enums):
    return type('Enum', (), enums)
85 hoặc
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
11, sẽ không được chuyển thành các thành viên và sẽ bị xóa khỏi lớp cuối cùng

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)
8

Ghi chú

def enum(**enums):
    return type('Enum', (), enums)
87 - Được sử dụng trong mã Python 2/3 để đảm bảo thứ tự thành viên nhất quán (thuộc tính lớp, bị xóa trong quá trình tạo lớp)

def enum(**enums):
    return type('Enum', (), enums)
88 - Được API chức năng sử dụng và bằng
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

7 để có được giá trị phù hợp cho một thành viên enum; có thể bị ghi đè

Mới trong phiên bản 3.6:

def enum(**enums):
    return type('Enum', (), enums)
83,
def enum(**enums):
    return type('Enum', (), enums)
87,
def enum(**enums):
    return type('Enum', (), enums)
88 will be normal attributes in Python 3.11 instead of either an error or a member (depending on if the name ends with an underscore). Using these names in 3.10 will issue a
def enum(**enums):
    return type('Enum', (), enums)
96.

Mới trong phiên bản 3.7:
class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4
43

Để giúp giữ mã Python 2 / Python 3 đồng bộ hóa một thuộc tính

def enum(**enums):
    return type('Enum', (), enums)
87 có thể được cung cấp. Nó sẽ được kiểm tra theo thứ tự thực tế của bảng liệt kê và gây ra lỗi nếu hai người không khớp:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)
9

Ghi chú

Trong mã Python 2, thuộc tính

def enum(**enums):
    return type('Enum', (), enums)
87 là cần thiết vì thứ tự định nghĩa bị mất trước khi có thể ghi lại.

_Private__names¶

Tên riêng sẽ là các thuộc tính bình thường trong Python 3.11 thay vì lỗi hoặc thành viên (tùy thuộc vào việc tên kết thúc bằng dấu gạch dưới). Sử dụng các tên này trong 3.10 sẽ phát hành
def enum(**enums):
    return type('Enum', (), enums)
96.

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 Kiểu thành viên

>>> Numbers.reverse_mapping['three']
'THREE'
0

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 Thành viên là trường hợp của lớp
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 của họ và thường được truy cập là
>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
00. Trong một số trường hợp nhất định, chúng cũng có thể được truy cập là
>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
01, nhưng bạn không bao giờ nên làm điều này vì tra cứu đó có thể thất bại hoặc tệ hơn, trả lại một cái gì đó bên cạnh thành viên
from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 mà bạn đang tìm ):

Hành vi này không được chấp nhận và sẽ bị xóa trong 3.11.

Nếu bạn cung cấp các phương thức phụ

from typing import Literal #python >=3.8
from typing_extensions import Literal #python 2.7, 3.4-3.7


Animal = Literal['ant', 'bee', 'cat', 'dog']

def hello_animal(animal: Animal):
    print(f"hello {animal}")

hello_animal('rock') # error
hello_animal('bee') # passes

2 của bạn, như lớp Planet ở trên, các phương thức đó sẽ hiển thị trong một
>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
13 của thành viên, nhưng không phải của lớp:

>>> Numbers.reverse_mapping['three']
'THREE'
1

Kết hợp các thành viên của ________ 84¶

Nếu sự kết hợp của các thành viên cờ không được đặt tên,

def enum(**enums):
    return type('Enum', (), enums)
40 sẽ bao gồm tất cả các cờ được đặt tên và tất cả các kết hợp các cờ có tên trong giá trị:

>>> Numbers.reverse_mapping['three']
'THREE'
2

Ghi chú

Trong 3.11, các kết hợp cờ không được đặt tên sẽ chỉ tạo ra các thành viên cờ chính tắc (còn gọi là cờ có giá trị đơn).Vì vậy,

>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
16 sẽ tạo ra một cái gì đó như
>>> Numbers = enum(ONE=1, TWO=2, THREE='three')
>>> Numbers.ONE
1
>>> Numbers.TWO
2
>>> Numbers.THREE
'three'
17.