On 06/07/2013 11:45 PM, Steven D'Aprano wrote:
On 08/06/13 15:18, Stephen J. Turnbull wrote:
Ethan Furman writes:

  > Enumerations can be pickled and unpickled::
  >
  >      >>> from enum import Enum
  >      >>> class Fruit(Enum):
  >      ...     tomato = 1
  >      ...     banana = 2
  >      ...     cherry = 3
  >      ...
  >      >>> from pickle import dumps, loads
  >      >>> Fruit.tomato is loads(dumps(Fruit.tomato))
  >      True
  > [...]
  > Still, it would be nice if this could work.

Well, you could cheat and reverse the test. ;-)

I assume the problem is that loads proceeds to recreate the Fruit
enum, rather than checking if there already is one?


I don't believe so. I understand that the problem is that pickle cannot find 
the Fruit enum in the __main__ module.

Untested, but adding this before the call to dumps might work:

import __main__
__main__.Fruit = Fruit


although that's the sort of thing that makes me think it's time to turn this 
into a unittest rather than a doctest.

Indeed, and it is already in several different ways. But it would be nice to have a pickle example in the docs that worked with doctest.

I ended up doing what Barry did:

    >>> from test.test_enum import Fruit
    >>> from pickle import dumps, loads
    >>> Fruit.tomato is loads(dumps(Fruit.tomato))
    True

--
~Ethan~
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to