On this page
enum.property
Prev: enum.pickle_by_global_name | Up: enum | Next: enum.ReprEnum
Rel: enum.auto, enum.Enum, enum.EnumCheck, enum.EnumDict
Ref: docs.python.org — enum.property
Tags: #public #python3 #stdlib #enum
Import
import enum
from enum import property
Signature & Overview
enum.property(fget=None, fset=None, fdel=None, doc=None)
This is a descriptor, used to define attributes that act differently when accessed through an enum member and through an enum class.
Methods & Attributes
property.deleter(self, fdel)—property.deletermethod/property.property.getter(self, fget)—property.gettermethod/property.property.setter(self, fset)—property.settermethod/property.
Usage
import enum
class Planet(enum.Enum):
EARTH = (1, "blue")
@enum.property
def color(self) -> str:
return self.value[1]
assert Planet.EARTH.color == "blue"
Gotchas
- Python 3.11+
DynamicClassAttributealias: Works like built-in@property, but lets you still have an enum member with the same name as a property without collision.
REPL Inspect
from enum import property
help(property)
Linked from
Related notes
- tuts/python3/stdlib_module/enum/pickle_by_global_name Linked here; links to this page; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/ReprEnum Linked here; links to this page; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/auto Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/Enum Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/EnumCheck Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/EnumDict Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/enum/EnumMeta shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/EnumType shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/Flag shared tags: #enum, #stdlib