Re: [Python 2.x] Pickling a datetime.tzinfo subclass instance?

Discussion in 'Python' started by Cong Ma, Dec 9, 2008.

  1. Cong Ma

    Cong Ma Guest

    Gabriel Genellina wrote:
    > En Mon, 08 Dec 2008 12:34:03 -0200, Cong Ma <> escribió:
    >
    >> I'm writing a program that pickles an instance of a custom subclass of
    >> datetime.tzinfo. I followed the guides given in the Library Reference
    >> (version
    >> 2.5.2, chapter 5.1.6), which contain the note:
    >>
    >> "Special requirement for pickling: A tzinfo subclass must have an
    >> __init__
    >> method that can be called with no arguments, else it can be pickled
    >> but possibly
    >> not unpickled again. This is a technical requirement that may be
    >> relaxed in the
    >> future."
    >>
    >> I tried this with an example "FixedOffset" subclass instance given in the
    >> Example section in the manual. It indeed failed to unpickle. To work
    >> around
    >> this, I found two possible solutions:
    >> 1. Modify the __init__ method so that it takes optional arguments with
    >> default
    >> values;

    >
    > Doing that still works with 2.6 and 3.0
    >
    >> 2. Implement the __getinitargs__ method so that it does the opposite of
    >> __init__: returning a tuple from the instance's internal state that
    >> can be used
    >> to re-initialize an instance, retaining the old value.

    >
    > In fact, it doesn't matter *what* it returns, as far as they're valid
    > arguments to __init__
    >
    >> My questions:
    >> 1. Is the "technical limitation" fixed in version 2.6 or 3.0? I can't
    >> check it
    >> for myself now... Python.org seems down and I can't find the docs.

    >
    > No, they behave the same (odd) way.
    >
    >> 2. To stick with version 2.5, which of the above 2 methods is better?
    >> Both seems
    >> to unpickle to the correct result, but are there subtle side-effects?
    >> Or there
    >> are better solutions?

    >
    > I'd use method 1, just because the __getinitargs__ are useless.
    >


    Gabriel,

    Thank you so much for your great explanations. Had you not told me this, I would
    hardly be able to find it out by myself :)

    Regards,
    Cong.
     
    Cong Ma, Dec 9, 2008
    #1
    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. John Hunter

    datetime and tzinfo

    John Hunter, Sep 16, 2004, in forum: Python
    Replies:
    0
    Views:
    313
    John Hunter
    Sep 16, 2004
  2. Tino Lange
    Replies:
    1
    Views:
    459
    Lawrence D'Oliveiro
    Apr 3, 2006
  3. Cong Ma
    Replies:
    0
    Views:
    460
    Cong Ma
    Dec 8, 2008
  4. Michael Torrie
    Replies:
    0
    Views:
    408
    Michael Torrie
    Mar 12, 2010
  5. aspineux
    Replies:
    3
    Views:
    316
    aspineux
    Oct 21, 2011
Loading...

Share This Page