Python 3.11+enumfunction
enum.pickle_by_global_name
pickle
A pickle reducer that serializes a globalized enum member by its module-global name.
Syntax
member.__reduce_ex__ = enum.pickle_by_global_name
This one only fits enums decorated with @global_enum, whose members also live in the module globals: the pickle then stores a plain module.NAME reference. If that global name is later removed or renamed, unpickling fails during lookup, so for ordinary non-globalized enums use pickle_by_enum_name instead.
Examples
import enum
print(callable(enum.pickle_by_global_name)) # → True
Color = enum.Enum('Color', 'RED GREEN')
print(enum.pickle_by_global_name(Color.GREEN, 2)) # → GREEN
print(isinstance(enum.pickle_by_global_name(Color.GREEN, 2), str)) # → True
print(Color.GREEN.__reduce_ex__(2)[1]) # → (2,)
print(enum.pickle_by_enum_name(Color.GREEN, 2)[0] is getattr) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy