Some clarifications about pickling of Enums
This commit is contained in:
parent
1c38ea5dc3
commit
2bd6510b87
37
pep-0435.txt
37
pep-0435.txt
|
@ -415,6 +415,21 @@ Some rules:
|
|||
and don't specify another data type such as ``int`` or ``str``.
|
||||
|
||||
|
||||
Pickling
|
||||
--------
|
||||
|
||||
Enumerations be pickled and unpickled::
|
||||
|
||||
>>> from enum.tests.fruit import Fruit
|
||||
>>> from pickle import dumps, loads
|
||||
>>> Fruit.tomato is loads(dumps(Fruit.tomato))
|
||||
True
|
||||
|
||||
The usual restrictions for pickling apply: picklable enums must be defined in
|
||||
the top level of a module, since unpickling requires them to be imporatable
|
||||
from that module.
|
||||
|
||||
|
||||
Functional API
|
||||
--------------
|
||||
|
||||
|
@ -431,9 +446,10 @@ The ``Enum`` class is callable, providing the following functional API::
|
|||
[<Animal.ant: 1>, <Animal.bee: 2>, <Animal.cat: 3>, <Animal.dog: 4>]
|
||||
|
||||
The semantics of this API resemble ``namedtuple``. The first argument
|
||||
of the call to ``Enum`` is the name of the enumeration. To support
|
||||
pickling of these enums, the module name can be specified using the
|
||||
``module`` keyword-only argument. E.g.::
|
||||
of the call to ``Enum`` is the name of the enumeration. Pickling enums
|
||||
created with the functional API will work on CPython and PyPy, but for
|
||||
IronPython and Jython you may need to specify the module name explicitly
|
||||
as follows::
|
||||
|
||||
>>> Animals = Enum('Animals', 'ant bee cat dog', module=__name__)
|
||||
|
||||
|
@ -452,21 +468,6 @@ assignment to ``Animal`` is equivalent to::
|
|||
... dog = 4
|
||||
|
||||
|
||||
Pickling
|
||||
--------
|
||||
|
||||
Enumerations be pickled and unpickled::
|
||||
|
||||
>>> from enum.tests.fruit import Fruit
|
||||
>>> from pickle import dumps, loads
|
||||
>>> Fruit.tomato is loads(dumps(Fruit.tomato))
|
||||
True
|
||||
|
||||
The usual restrictions for pickling apply: picklable enums must be defined in
|
||||
the top level of a module, to be importable from that module when unpickling
|
||||
occurs.
|
||||
|
||||
|
||||
Proposed variations
|
||||
===================
|
||||
|
||||
|
|
Loading…
Reference in New Issue