[Python-Dev] PEP 435: pickling enums created with the functional API

Steven D'Aprano steve at pearwood.info
Wed May 8 03:00:45 CEST 2013


On 07/05/13 23:34, Eli Bendersky 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.


Does this issue really need to be solved before 435 is accepted? As the Zen says:

Now is better than never.
Although never is often better than *right* now.

Solving the pickle issue is a hard problem, but not a critical issue. namedtuple has had the same issue since its inception, only worse because there is no class syntax for namedtuple. This has not been a barrier to the success of namedtuple.

Or rather, the issue is not with Enum, or namedtuple, but pickle. Any dynamically-created type will have this issue:

>>> import pickle
>>> def example(name):
...     return type(name, (object,), {})
...
>>> instance = example("Foo")()
>>> pickle.dumps(instance)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
_pickle.PicklingError: Can't pickle <class '__main__.Foo'>: attribute lookup __main__.Foo failed


I don't think it is unreasonable to chalk it up to a limitation of pickle, and say that unless you can meet certain conditions, you won't be able to pickle your instance.

Either way, approval of PEP 435 should not be dependent on fixing the pickle issue.




-- 
Steven


More information about the Python-Dev mailing list