Hướng dẫn python enum __str__ - python enum __str__

Chuyển đổi enum thành một chuỗi trong python #

Để chuyển đổi một enum thành một chuỗi trong Python:

  1. Truy cập tên của enum, ví dụ: Color.RED.name.
  2. Truy cập giá trị của enum, ví dụ: Color.RED.value.
  3. Tùy chọn, sử dụng lớp str() để chuyển đổi giá trị thành một chuỗi.

Copied!

from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' # 👇️ human readable string representation print(Color.RED) # 👉️ Color.RED # 👇️ repr() shows the value as well print(repr(Color.RED)) # 👉️ # 👇️ get name of enum print(Color.RED.name) # 👉️ RED # 👇️ get value of enum print(Color.RED.value) # 👉️ stop # 👇️ if enum value is int, you can convert it to str print(str(Color.RED.value)) # 👉️ stop

Bạn có thể sử dụng các thuộc tính namevalue trên một thành viên enum để lấy tên và giá trị của Enum.

Nếu giá trị không phải là một chuỗi và bạn cần chuyển đổi nó thành một, hãy chuyển nó sang lớp str().

Ngoài ra, bạn có thể thực hiện phương thức

Copied!

from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' def __str__(self): return str(self.value) print(Color.RED) # 👉️ "stop"
1 trong lớp.

Copied!

from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' def __str__(self): return str(self.value) print(Color.RED) # 👉️ "stop"

Phương thức __str __ () được gọi bởi

Copied!

from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' def __str__(self): return str(self.value) print(Color.RED) # 👉️ "stop"
2 và các hàm tích hợp

Copied!

from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' def __str__(self): return str(self.value) print(Color.RED) # 👉️ "stop"
3 và

Copied!

from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' def __str__(self): return str(self.value) print(Color.RED) # 👉️ "stop"
4 và trả về biểu diễn chuỗi không chính thức của đối tượng.

Bây giờ bạn có thể nhận được giá trị của enum trực tiếp, mà không cần truy cập thuộc tính value trên thành viên ENUM.

Bạn cũng có thể sử dụng dấu ngoặc vuông để truy cập các thành viên ENUM.

Copied!

from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' name = 'RED' print(Color[name].value) # 👉️ 'stop' print(Color['RED'].value) # 👉️ 'stop'

Điều này rất hữu ích khi bạn không biết tên của thành viên enum trước thời hạn (vì nó được đọc từ một tệp hoặc được tìm nạp từ API).

Bạn có thể sử dụng một vòng lặp

Copied!

from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' def __str__(self): return str(self.value) print(Color.RED) # 👉️ "stop"
6 đơn giản nếu bạn cần lặp lại một enum.

Copied!

from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' for color in Color: print(color) print(color.name, color.value)

Bạn có thể sử dụng một danh sách hiểu để kiểm tra xem một giá trị cụ thể có trong enum không.

Copied!

from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' # 👇️ check enum member type print(type(Color.RED)) # 👉️ # 👇️ check if member belongs to Enum print(isinstance(Color.RED, Color)) # 👉️ True values = [member.value for member in Color] print(values) # 👉️ ['stop', 'go', 'get ready'] if 'stop' in values: # 👇️ this runs print('stop is in values')

Danh sách các hệ thống được sử dụng để thực hiện một số hoạt động cho mọi yếu tố hoặc chọn một tập hợp con của các phần tử đáp ứng một điều kiện.