<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, May 7, 2013 at 8:03 AM, Nick Coghlan <span dir="ltr"><<a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class=""><div class="h5">On Tue, May 7, 2013 at 11:34 PM, Eli Bendersky <<a href="mailto:eliben@gmail.com">eliben@gmail.com</a>> wrote:<br>


> One of the contended issues with PEP 435 on which Guido pronounced was the<br>
> functional API, that allows created enumerations dynamically in a manner<br>
> similar to namedtuple:<br>
><br>
>   Color = Enum('Color', 'red blue green')<br>
><br>
> The biggest complaint reported against this API is interaction with pickle.<br>
> As promised, I want to discuss here how we're going to address this concern.<br>
><br>
> At this point, the pickle docs say that module-top-level classes can be<br>
> pickled. This obviously works for the normal Enum classes, but is a problem<br>
> with the functional API because the class is created dynamically and has no<br>
> __module__.<br>
><br>
> To solve this, the reference implementation is used the same approach as<br>
> namedtuple (*). In the metaclass's __new__ (this is an excerpt, the real<br>
> code has some safeguards):<br>
><br>
>   module_name = sys._getframe(1).f_globals['__name__']<br>
>   enum_class.__module__ = module_name<br>
><br>
> According to an earlier discussion, this is works on CPython, PyPy and<br>
> Jython, but not on IronPython. The alternative that works everywhere is to<br>
> define the Enum like this:<br>
><br>
>   Color = Enum('the_module.Color', 'red blue green')<br>
><br>
> The reference implementation supports this as well.<br>
><br>
> Some points for discussion:<br>
><br>
> 1) We can say that using the functional API when pickling can happen is not<br>
> recommended, but maybe a better way would be to just explain the way things<br>
> are and let users decide?<br>
<br>
</div></div>It's probably worth creating a section in the pickle docs and<br>
explaining the vagaries of naming things and the dependency on knowing<br>
the module name. The issue comes up with defining classes in __main__<br>
and when implementing pseudo-modules as well (see PEP 395).<br>
<div class="im"><br>
> 2) namedtuple should also support the fully qualified name syntax. If this<br>
> is agreed upon, I can create an issue.<br>
<br>
</div>Yes, I think that part should be done.<br>
<div class="im"><br></div></blockquote><div><br><a href="http://bugs.python.org/issue17941">http://bugs.python.org/issue17941</a><br><br></div><div>Eli</div><br></div><br></div></div>