[Python-Dev] PEP 435: pickling enums created with the functional API (original) (raw)

Eli Bendersky eliben at gmail.com
Thu May 9 05:47:46 CEST 2013


On Tue, May 7, 2013 at 8:03 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:

On Tue, May 7, 2013 at 11:34 PM, Eli Bendersky <eliben at gmail.com> wrote: > One of the contended issues with PEP 435 on which Guido pronounced was the > functional API, that allows created enumerations dynamically in a manner > similar to namedtuple: > > Color = Enum('Color', 'red blue green') > > The biggest complaint reported against this API is interaction with pickle. > As promised, I want to discuss here how we're going to address this concern. > > At this point, the pickle docs say that module-top-level classes can be > pickled. This obviously works for the normal Enum classes, but is a problem > with the functional API because the class is created dynamically and has no > module. > > To solve this, the reference implementation is used the same approach as > namedtuple (*). In the metaclass's new (this is an excerpt, the real > code has some safeguards): > > modulename = sys.getframe(1).fglobals['name'] > enumclass.module = modulename > > 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('themodule.Color', 'red blue green') > > The reference implementation supports this as well. > > Some points for discussion: > > 1) We can say that using the functional API when pickling can happen is not > recommended, but maybe a better way would be to just explain the way things > are and let users decide?

It's probably worth creating a section in the pickle docs and explaining the vagaries of naming things and the dependency on knowing the module name. The issue comes up with defining classes in main and when implementing pseudo-modules as well (see PEP 395). > 2) namedtuple should also support the fully qualified name syntax. If this > is agreed upon, I can create an issue. Yes, I think that part should be done. http://bugs.python.org/issue17941

Eli -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20130508/498c456d/attachment.html>



More information about the Python-Dev mailing list