[Python-checkins] peps: Some clarifications about pickling of Enums

eli.bendersky python-checkins at python.org
Sat May 11 19:20:13 CEST 2013


http://hg.python.org/peps/rev/85a0892a4b5c
changeset:   4885:85a0892a4b5c
user:        Eli Bendersky <eliben at gmail.com>
date:        Sat May 11 10:19:02 2013 -0700
summary:
  Some clarifications about pickling of Enums

files:
  pep-0435.txt |  37 +++++++++++++++++++------------------
  1 files changed, 19 insertions(+), 18 deletions(-)


diff --git a/pep-0435.txt b/pep-0435.txt
--- a/pep-0435.txt
+++ b/pep-0435.txt
@@ -415,6 +415,21 @@
    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 @@
     [<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 @@
     ...   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
 ===================
 

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list