Klasse "EnumField"
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Die EnumField-Klasse stellt Definitionen für Enumerationswerte bereit. Enumerationsfelder können über Standardwerte verfügen, die verzögert werden, bis der zugehörige Enumerationstyp erkannt wurde. Dies ist für die Unterstützung bestimmter Zirkelverweise erforderlich. Beispiele:
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
wird vom Modul protorpc.messages
bereitgestellt.
Konstruktor
Der Konstruktor der EnumField-Klasse wird folgendermaßen definiert:
- class EnumField(enum_type, number, required, repeated, variant, default)
-
Stellt eine Felddefinition für Enumerationswerte bereit.
Argumente
- enum_type
- Der Enumerationstyp eines Felds. Muss eine abgeleitete Klasse von Enum sein.
- number
- Die Nummer des Felds. Muss für jede Nachrichtenklasse eindeutig sein.
- required
- Gibt an, ob es sich bei dem Feld um ein Pflichtfeld handelt. Dieses Argument und das Argument
repeated
schließen sich gegenseitig aus. Geben Sie repeated
nicht an, wenn Sie required
verwenden.
- repeated
- Gibt an, ob dieses Feld wiederholt wird. Dieses Argument und das Argument
required
schließen sich gegenseitig aus. Geben Sie required
nicht an, wenn Sie repeated
verwenden.
- variant
- Beschreibt den Feldtyp genauer. Einige Feldtypen werden anhand des zugrunde liegenden Sendeformats weiter eingeschränkt. Am besten verwenden Sie den Standardwert, Entwickler können mit diesem Feld jedoch auch ein Ganzzahlenfeld als 32-Bit-Ganzzahl im Gegensatz zum 64-Bit-Standard deklarieren.
- Standardeinstellung
- Standardwert zur Verwendung für das Feld, falls im Stream nichts gefunden wird.
Löst einen FieldDefinitionError aus, wenn enum_type
ungültig ist.
Klasseneigenschaften
Die EnumField-Klasse umfasst die folgenden Klasseneigenschaften:
- type()
- Enumerationstyp für das Feld.
- default()
- Standardwert für das Enumerationsfeld. Wenn der Standardwert nicht aufgelöst ist, wird Enum type als Standard verwendet.
Instanzmethoden
EnumField-Instanzen weisen die folgenden Methoden auf:
- validate_default_element(value)
- Validiert das Standardelement des Enumerationsfelds. Enumerationsfelder erlauben eine verzögerte Auflösung von Standardwerten, wenn der Feldtyp nicht erkannt wurde. Bei dem Standardwert eines Felds kann es sich um einen String oder eine Ganzzahl handeln. Wenn der Enumerationstyp des Felds erkannt wurde, wird der Standardwert gegen diesen Typ validiert.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
Zuletzt aktualisiert: 2025-09-04 (UTC).
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 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."]]