Python 2.7 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Python 2.7 應用程式,即使貴機構先前曾使用機構政策重新啟用舊版執行階段的部署作業,也無法部署。現有的 Python 2.7 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
改用系統支援的最新 Python 版本。
EnumField 類別
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
EnumField 類別為列舉值提供定義。列舉欄位的預設值可能會延遲到關聯的列舉類型完成解析後才會解析,這對某些特定的循環參照而言是必須的。例如:
from protorpc import messages
class Message1(messages.Message):
class Color(messages.Enum):
RED = 1
GREEN = 2
BLUE = 3
# Validate this field's default value when default is accessed.
animal = messages.EnumField('Message2.Animal', 1, default='HORSE')
class Message2(messages.Message):
class Animal(messages.Enum):
DOG = 1
CAT = 2
HORSE = 3
# This fields default value will be validated right away since Color is
# already fully resolved.
color = messages.EnumField(Message1.Color, 1, default='RED')
EnumField
由 protorpc.messages
模組提供。
建構函式
EnumField 類別的建構函式定義如下:
- class EnumField(enum_type, number, required, repeated, variant, default)
-
為 Enum 值提供欄位定義。
引數
- enum_type
- 欄位的列舉型別。必須是 Enum 的子類別。
- number
- 欄位編號。每種訊息類別的欄位號碼皆必須與其他類別不同。
- 必填
- 這個欄位是否為必填欄位。與
repeated
引數互斥;若使用 required
,請勿指定 repeated
。
- 重複
- 這個欄位是否為重複欄位。與
required
引數互斥;若使用 repeated
,請勿指定 required
。
- variant
- 進一步指定欄位類型。有些欄位類型由於基本傳輸格式而無法進一步指定。最佳做法是使用預設值,但開發人員可以使用這個欄位,將整數欄位宣告為 32 位元整數,而非預設的 64 位元。
- 預設
- 如果在串流中找不到該欄位,則使用的預設值。
如果 enum_type
無效,則會引發 FieldDefinitionError。
類別屬性
EnumField 類別提供下列類別屬性:
- type()
- 用於欄位的列舉型別。
- default()
- 列舉欄位的預設值。如果系統無法解析預設值,會使用 Enum 類型做為預設值。
例項方法
EnumField 例項有下列方法:
- validate_default_element(value)
- 驗證整數欄位的預設元素。當系統尚未解析某個欄位類型時,列舉欄位允許延後解析預設值的時間。欄位的預設值可能是字串或整數。如果系統已解析欄位的 Enum 類型,就會依據該類型驗證預設值。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-11 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-11 (世界標準時間)。"],[[["\u003cp\u003eThe \u003ccode\u003eEnumField\u003c/code\u003e class defines fields for enum values, supporting delayed default value resolution to handle circular references.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eEnumField\u003c/code\u003e is constructed using the enum type, field number, and optional arguments like \u003ccode\u003erequired\u003c/code\u003e, \u003ccode\u003erepeated\u003c/code\u003e, \u003ccode\u003evariant\u003c/code\u003e, and \u003ccode\u003edefault\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003etype()\u003c/code\u003e property returns the Enum type associated with the field, while \u003ccode\u003edefault()\u003c/code\u003e provides the default enum value, or the enum type if the value is unresolved.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003evalidate_default_element(value)\u003c/code\u003e is used to validate the default value, supporting strings or integers, against the resolved Enum type.\u003c/p\u003e\n"],["\u003cp\u003eThe default value for an \u003ccode\u003eEnumField\u003c/code\u003e can be validated at the time it is accessed, and it is provided by the \u003ccode\u003eprotorpc.messages\u003c/code\u003e module.\u003c/p\u003e\n"]]],[],null,[]]