Python 2.7 はサポートが終了しており、2026 年 1 月 31 日に
非推奨になります。非推奨になると、過去に組織のポリシーを使用して以前のランタイムのデプロイを再度有効にしていた場合でも、Python 2.7 アプリケーションをデプロイできなくなります。既存の Python 2.7 アプリケーションは、
非推奨日以降も引き続き実行され、トラフィックを受信します。
サポートされている最新バージョンの Python に移行することをおすすめします。
EnumField クラス
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
EnumField クラスは、列挙値の定義を提供します。Enum フィールドは、関連付けられた列挙型が解決されるまで遅延されるデフォルト値を持つことができます。これは特定の循環参照をサポートするために必要です。次に例を示します。
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 型。Enum のサブクラスにする必要があります。
- number
- フィールドの番号。メッセージ クラスごとに一意でなければなりません。
- required
- このフィールドが必須かどうかを指定します。
repeated
引数と同時に指定することはできません。required
を使用する場合は repeated
を指定しないでください。
- repeated
- このフィールドが反復フィールドかどうかを指定します。
required
引数と同時に指定することはできません。repeated
を使用する場合は required
を指定しないでください。
- variant
- フィールドの型を指定します。一部のフィールド型は、基になる送信形式に基づいて制約できます。デフォルト値の使用をおすすめしますが、このフィールドを使用してデフォルトの 64 ビットではなく 32 ビットの整数として整数フィールドを宣言することもできます。
- default
- ストリーム内にフィールドの値が見つからない場合に使用するデフォルト値。
enum_type
が無効な場合、FieldDefinitionError が発生します。
クラスのプロパティ
EnumField クラスには次のクラス プロパティがあります:
- type()
- フィールドに使用する列挙型。
- default()
- 列挙フィールドのデフォルト値です。デフォルト値が解決されない場合は、デフォルトとして Enum 型を使用します。
インスタンス メソッド
EnumField のインスタンスには次のメソッドがあります。
- validate_default_element(value)
- Enum フィールドのデフォルト要素を検証します。Enum フィールドでは、フィールドの型が解決されていない場合にデフォルト値の遅延解決が可能です。フィールドのデフォルト値は文字列または整数になります。フィールドの Enum 型が解決されている場合、その型に基づいてデフォルト値が検証されます。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-09-04 UTC。
[[["わかりやすい","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-09-04 UTC。"],[[["\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,["# The EnumField Class\n\nThe EnumField class provides definitions for enum values. Enum fields may have default values that are delayed until the associated enum type is resolved. This is necessary to support certain circular references. For example: \n\n```python\nfrom protorpc import messages\n\nclass Message1(messages.Message):\n\n class Color(messages.Enum):\n RED = 1\n GREEN = 2\n BLUE = 3\n\n # Validate this field's default value when default is accessed.\n animal = messages.EnumField('Message2.Animal', 1, default='HORSE')\n\nclass Message2(messages.Message):\n\n class Animal(messages.Enum):\n DOG = 1\n CAT = 2\n HORSE = 3\n\n # This fields default value will be validated right away since Color is\n # already fully resolved.\n color = messages.EnumField(Message1.Color, 1, default='RED')\n```\n\n`EnumField` is provided by the `protorpc.messages` module.\n\nConstructor\n-----------\n\nThe constructor of the EnumField class is defined as follows:\n\nclass EnumField(enum_type, number, required, repeated, variant, default)\n\n: Provides a field definition for Enum values.\n\n **Arguments**\n\n enum_type\n : The Enum type for a field. Must be a subclass of [Enum](/appengine/docs/legacy/standard/python/tools/protorpc/messages/enumclass).\n\n number\n : The number of the field. Must be unique per message class.\n\n required\n : Whether or not this field is required. Mutually exclusive with the `repeated` argument; do not specify `repeated` if you use `required`.\n\n repeated\n : Whether or not this field is repeated. Mutually exclusive with the `required` argument; do not specify `required` if you use `repeated`.\n\n variant\n : Further specifies the type of field. Some field types are further restrained based on the underlying wire format. Best practice is to use the default value, but developers can use this field to declare an integer field as a 32-bit integer vs. the default 64 bit.\n\n default\n : Default value to use for the field if it is not found in stream.\n\n Raises a [FieldDefinitionError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#FieldDefinitionError) when `enum_type` is invalid.\n\nClass Properties\n----------------\n\nThe EnumField class provides the following class properties:\n\ntype()\n: Enum type used for the field.\n\ndefault()\n: Default for the enum field. If the default value is unresolved, uses Enum [type](#type) as the default.\n\nInstance Methods\n----------------\n\nEnumField instances have the following method:\n\nvalidate_default_element(value)\n: Validates the default element of the Enum field. Enum fields allow for delayed resolution of default values when the type of the field has not been resolved. The default value of a field may be a string or an integer. If the Enum type of the field has been resolved, the default value is validated against that type."]]