Python Glossary Русский

Python 3.11+enumfunction

enum.global_enum

globalization

A decorator for an enum class: it copies the members into the module's global namespace (usable without the prefix) and gives them a module-style repr.

Syntax

@enum.global_enum

The decorator exists for the standard library, where constants historically sat at module level (re.IGNORECASE, socket.AF_INET) and the enum class arrived later — it adds the class without breaking the old access path. In your own code it is rarely a good idea: the names land in globals() without an actual assignment, so linters and IDE completion miss them, and a name clash silently overwrites an existing module-level variable.

Examples

import enum, re
print(callable(enum.global_enum))   # → True
print(repr(re.IGNORECASE))   # → re.IGNORECASE
import enum, re
@enum.global_enum
class Color(enum.Enum): RED = 1; GREEN = 2
print(RED is Color.RED)   # → True
print(RED)   # → RED

See also

Open in the full glossarydocs.python.orgReport an inaccuracy