<p dir="ltr"><br>
On 9 May 2013 13:48, "Eli Bendersky" <<a href="mailto:eliben@gmail.com">eliben@gmail.com</a>> wrote:<br>
><br>
><br>
><br>
><br>
> On Tue, May 7, 2013 at 8:03 AM, Nick Coghlan <<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>> wrote:<br>
>><br>
>> 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>
>> 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>
>><br>
>> > 2) namedtuple should also support the fully qualified name syntax. If this<br>
>> > is agreed upon, I can create an issue.<br>
>><br>
>> Yes, I think that part should be done.<br>
>><br>
><br>
> <a href="http://bugs.python.org/issue17941">http://bugs.python.org/issue17941</a></p>
<p dir="ltr">As Eric noted on the tracker issue, a keyword only "module" argument may be a better choice for both than allowing dotted names. A separate parameter is easier to use with __name__ to avoid hardcoding the module name.</p>

<p dir="ltr">At the very least, the PEP should provide a rationale for the current choice.</p>
<p dir="ltr">Cheers,<br>
Nick.</p>
<p dir="ltr">><br>
> Eli<br>
><br>
><br>
</p>