How to pickle a subclass of tuple?

Discussion in 'Python' started by Christos TZOTZIOY Georgiou, Jun 3, 2004.

  1. __getstate__ is easy:

    def __getstate__(self):
    return tuple(self)

    but even

    def __getstate__(self):
    return self

    seems to work, as far as Pickle.dump is concerned. The problem is, how
    one writes a __setstate__ for an immutable class?
    --
    TZOTZIOY, I speak England very best,
    "I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses
    Christos TZOTZIOY Georgiou, Jun 3, 2004
    #1
    1. Advertising

  2. Christos TZOTZIOY Georgiou wrote:

    > __getstate__ is easy:
    >
    > def __getstate__(self):
    > return tuple(self)
    >
    > but even
    >
    > def __getstate__(self):
    > return self
    >
    > seems to work, as far as Pickle.dump is concerned. The problem is, how
    > one writes a __setstate__ for an immutable class?


    Why do you need to do this i.e. redefine __getstate__() and __setstate__()?

    Doesn't just pickling instances of your class work?

    >>> class T(tuple):pass

    ....
    >>> t = T((1,2))
    >>> t.a = 33
    >>> import pickle
    >>> x = pickle.loads(pickle.dumps(t))
    >>> x

    (1, 2)
    >>> x.a

    33
    >>>


    --
    Shalabh
    Shalabh Chaturvedi, Jun 4, 2004
    #2
    1. Advertising

  3. On Thu, 03 Jun 2004 17:52:45 -0700, rumours say that Shalabh Chaturvedi
    <> might have written:

    >Christos TZOTZIOY Georgiou wrote:
    >
    >> __getstate__ is easy:
    >>
    >> def __getstate__(self):
    >> return tuple(self)
    >>
    >> but even
    >>
    >> def __getstate__(self):
    >> return self
    >>
    >> seems to work, as far as Pickle.dump is concerned. The problem is, how
    >> one writes a __setstate__ for an immutable class?

    >
    >Why do you need to do this i.e. redefine __getstate__() and __setstate__()?
    >
    >Doesn't just pickling instances of your class work?
    >
    > >>> class T(tuple):pass

    >...
    > >>> t = T((1,2))
    > >>> t.a = 33
    > >>> import pickle
    > >>> x = pickle.loads(pickle.dumps(t))
    > >>> x

    >(1, 2)
    > >>> x.a

    >33


    Oh, yeah, I forgot to mention that I also define __slots__ to avoid
    having a dictionary; I do that because I need to create *really* lots of
    instances, and the memory usage became prohibiting for the specific
    computer I want this to run (a firewall).

    It's data about various of ip addresses related to an anti-spam program.
    I want to cache results to avoid running DNS requests every time.

    So, apart from advice on how to circumvent this for my specific problem
    (eg, run on another computer, don't base on tuple etc) since I already
    have circumvented it, I would still like to find out if it is possible
    or not to pickle a tuple-based class with __slots__ defined.
    --
    TZOTZIOY, I speak England very best,
    "I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses
    Christos TZOTZIOY Georgiou, Jun 4, 2004
    #3
  4. Christos TZOTZIOY Georgiou

    Greg Chapman Guest

    On Thu, 03 Jun 2004 19:26:37 +0300, Christos "TZOTZIOY" Georgiou
    <> wrote:

    >__getstate__ is easy:
    >
    >def __getstate__(self):
    > return tuple(self)
    >
    >but even
    >
    >def __getstate__(self):
    > return self
    >
    >seems to work, as far as Pickle.dump is concerned. The problem is, how
    >one writes a __setstate__ for an immutable class?


    You'll probably need to use some of the extended pickle protocol hooks
    documented here:

    http://www.python.org/peps/pep-0307.html

    In particular, you can give your subclass a __reduce__ implementation, which
    pretty much gives you complete control over how it is pickled and unpickled.

    ---
    Greg Chapman
    Greg Chapman, Jun 4, 2004
    #4
  5. On Fri, 04 Jun 2004 16:09:25 GMT, rumours say that Greg Chapman
    <> might have written:

    >You'll probably need to use some of the extended pickle protocol hooks
    >documented here:
    >
    >http://www.python.org/peps/pep-0307.html


    Thanks, Greg, I'm currently reading the PEP.
    --
    TZOTZIOY, I speak England very best,
    "I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses
    Christos TZOTZIOY Georgiou, Jun 6, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    431
    jstorta
    Feb 20, 2006
  2. Jimmy Retzlaff
    Replies:
    0
    Views:
    373
    Jimmy Retzlaff
    Oct 16, 2003
  3. a pickle's pickle

    , Aug 2, 2005, in forum: Python
    Replies:
    4
    Views:
    373
  4. Michele Simionato
    Replies:
    2
    Views:
    1,862
    Michele Simionato
    May 23, 2008
  5. James Stroud

    dict subclass and pickle bug (?)

    James Stroud, Dec 5, 2008, in forum: Python
    Replies:
    4
    Views:
    817
    James Stroud
    Dec 5, 2008
Loading...

Share This Page