problem pickling objects created with the type function

Discussion in 'Python' started by danny, Apr 21, 2004.

  1. danny

    danny Guest

    Howdy,

    I've run into a problem pickling objects created with a type
    statement. Here's the code:

    *********************************************
    import pickle

    class foo(object):
    def __init__(self):
    self.wombat = []

    def setWombat(self):
    self.wombat = [1]

    # pickle a foo instance
    x = foo()
    print pickle.dumps(x) # works

    # pickle a foobar instance
    childType = type('foobar',(foo,),{'slor':7})
    y = childType()
    print pickle.dumps(y) # raises an exception
    *********************************************

    I'm pretty sure the failure is because 'foobar' is never in the global
    namespace. If I change the code to foobar = type('foobar',... then
    the code works, but I dont' want to do this because I create the class
    name in a factory and it is mangled to prevent different invocations
    of the factory from having the same class name.

    Does anyone know how to get around this, or how to get 'foobar' =
    childType into the global namespace?

    thanks,
    Danny
     
    danny, Apr 21, 2004
    #1
    1. Advertising

  2. danny

    Jeff Epler Guest

    Yep.

    Pickle stores instances by pickling information about the instance, plus
    a string it uses to find the class.

    If you do something too clever, like you did above, it doesn't work.

    Jeff
     
    Jeff Epler, Apr 21, 2004
    #2
    1. Advertising

  3. danny

    Greg Chapman Guest

    On 21 Apr 2004 14:57:17 -0700, (danny) wrote:

    >I'm pretty sure the failure is because 'foobar' is never in the global
    >namespace. If I change the code to foobar = type('foobar',... then
    >the code works, but I dont' want to do this because I create the class
    >name in a factory and it is mangled to prevent different invocations
    >of the factory from having the same class name.
    >
    >Does anyone know how to get around this, or how to get 'foobar' =
    >childType into the global namespace?


    You should be able to make foobars pickleable without introducing foobar into
    the global namespace by using a __reduce__ method, see:

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

    expecially the section "Extended __reduce__ API".

    ---
    Greg Chapman
     
    Greg Chapman, Apr 23, 2004
    #3
    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. jimjim
    Replies:
    12
    Views:
    1,904
    Ron Natalie
    Jun 3, 2005
  2. Thomas Guettler

    Pickling Objects inherited from dict (Bug?)

    Thomas Guettler, Nov 5, 2003, in forum: Python
    Replies:
    1
    Views:
    303
    Michael Hudson
    Nov 5, 2003
  3. John Machin
    Replies:
    2
    Views:
    292
    Reinhold Birkenfeld
    Sep 3, 2005
  4. Shahin Saadati

    pickling objects in jython

    Shahin Saadati, Sep 7, 2005, in forum: Python
    Replies:
    0
    Views:
    307
    Shahin Saadati
    Sep 7, 2005
  5. Littlefield, Tyler
    Replies:
    7
    Views:
    134
    mblume
    Apr 6, 2013
Loading...

Share This Page