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()
- 用于字段的 Enum 类型。
- default()
- 枚举字段的默认值。如果未解析默认值,则使用 Enum 类型作为默认值。
实例方法
EnumField 实例提供以下方法:
- validate_default_element(value)
- 验证 Enum 字段的默认元素。在字段类型尚未解析时,Enum 字段允许延迟解析默认值。字段的默认值可以是一个字符串或一个整数。如果已解析字段的 Enum 类型,则将针对该类型验证默认值。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-04。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\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."]]