[Python-Dev] Pickling of Enums
ethan at stoneleaf.us
Tue Feb 18 18:11:35 CET 2014
On 02/15/2014 11:01 AM, Serhiy Storchaka wrote:
> How Enum items should be pickled, by value or by name?
> I think that Enum will be used to collect system-depending constants, so the value of AddressFamily.AF_UNIX can be 1 on
> one platform and 2 on other. If pickle enums by value, then pickled AddressFamily.AF_INET on on platform can be
> unpickled as AddressFamily.AF_UNIX on other platform. This looks weird and contrary to the nature of enums.
There is one more wrinkle to pickling by name (it's actually still there in pickle by value, just more obvious in pickle
by name) -- aliases. It seems to me the most common scenario to having a name represent different values on different
systems is when on system A they are different, but on system B they are the same:
value1 = 1
value2 = 2
value1 = 1
value2 = 1
If you're on system B there is no way to pickle (by name or value) value2 such that we get value2 back on system A. The
only way I know of to make that work would be to dispense with identity comparison, use the normal == comparison, and
have aliases actually be separate objects (we could still use singletons, but it would be one per name instead of the
current one per value, and it would also be an implementation detail).
More information about the Python-Dev