On Tue, May 7, 2013 at 8:03 AM, Nick Coghlan <ncogh...@gmail.com> wrote:
> On Tue, May 7, 2013 at 11:34 PM, Eli Bendersky <eli...@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): > > > > 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 > to > > 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 > 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
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com