[Python-Dev] PEP 435: pickling enums created with the functional API

Michael Urman murman at gmail.com
Tue May 7 20:15:11 CEST 2013


On Tue, May 7, 2013 at 8:34 AM, Eli Bendersky <eliben at gmail.com> wrote:

> According to an earlier discussion, this is works on CPython, PyPy and
> Jython, but not on IronPython. The alternative that works everywhere is to
> define the Enum like this:
>
>   Color = Enum('the_module.Color', 'red blue green')
>
> The reference implementation supports this as well.
>

As an alternate bikeshed color, why not pass the receiving module to the
class factory when pickle support is desirable? That should be less brittle
than its name. The class based syntax can still be recommended to libraries
that won't know ahead of time if their values need to be pickled.

>>> Color = Enum('Color', 'red blue green', module=__main__)

Functions that wrap class factories could similarly accept and pass a
module along.

The fundamental problem is that the class factory cannot know what the
intended destination module is without either syntax that provides this
('class' today, proposed 'def' or 'class from' in the thread, or the caller
passing additional information around (module name, or module instance).
Syntax changes are clearly beyond the scope of PEP 435, otherwise a true
enum syntax might have been born. So that leaves us with requiring the
caller to provide it.

Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130507/a48a85a6/attachment-0001.html>


More information about the Python-Dev mailing list