[Python-Dev] PEP 435: pickling enums created with the functional API
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
> > 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
> > As promised, I want to discuss here how we're going to address this
> > 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
> > with the functional API because the class is created dynamically and has
> > __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):
> > module_name = sys._getframe(1).f_globals['__name__']
> > enum_class.__module__ = module_name
> > According to an earlier discussion, this is works on CPython, PyPy and
> > Jython, but not on IronPython. The alternative that works everywhere is
> > define the Enum like this:
> > Color = Enum('the_module.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
> > recommended, but maybe a better way would be to just explain the way
> > 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
> > is agreed upon, I can create an issue.
> Yes, I think that part should be done.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-Dev