
On Thu, May 2, 2013 at 1:39 PM, Guido van Rossum <guido@python.org> wrote:
On Thu, May 2, 2013 at 12:07 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
In order for the Enum convenience function to be pickleable, we have
On Thu, May 2, 2013 at 1:18 PM, fwierzbicki@gmail.com <fwierzbicki@gmail.com> wrote: this
line of code in the metaclass:
enum_class.__module__ = sys._getframe(1).f_globals['__name__']
This works fine for Cpython, but what about the others? This should work for Jython, but I can't say I like it. I believe IronPython has a sort of speedup mode that disallows the use of _getframe, and I'd like to add this to Jython someday.
This particular function is typically only called at module load time, so speeding it up isn't worth it.
FWIW, as Eli pointed out, namedtuple() does the same thing (since Python 2.6), so we'll just copy that code (refactoring it doesn't have to hold up the PEP). The only other alternative I find acceptable is not to have the convenience API at all. That's Eli's call.
I really prefer having the convenience API and acknowledging that it has some limitations (i.e. picking enums that were created with the convenience API and are nested in classes).
[Eli]
Would nesting the non-convenience Enum in a function or a class allow one to pickle it? I think programmers who want their libraries to be pickle-able already have to be aware of some restrictions about what can and cannot be pickled.
Apparently it hasn't been a problem for namedtuple. Calling namedtuple() or Enum() in another function is similar to a class statement inside a function -- the resulting class isn't picklable.
(But from this, don't conclude that it's not important for namedtuple() or Enum() to return a picklable class. It is important. It is just not important to try to make it work when they are called through some other wrapper -- there's just not much use for such a pattern.)
I agree. Eli