Re: How to pickle bound methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • srinivasan srinivas

    Re: How to pickle bound methods

    Peter,
    Could you please explain the code breifly?? I am not getting what it does.
    Thanks,
    Srini 



    ----- Original Message ----
    From: Peter Otten <__peter__@web. de>
    To: python-list@python.org
    Sent: Wednesday, 2 July, 2008 12:53:19 PM
    Subject: Re: How to pickle bound methods

    srinivasan srinivas wrote:
    I would like to know how to pickle a bound method??
    $ cat pickle_method.p y
    import copy_reg
    import new

    def make_instanceme thod(inst, methodname):
        return getattr(inst, methodname)

    def pickle_instance method(method):
        returnmake_inst ancemethod, (method.im_self , method.im_func. __name__)

    copy_reg.pickle (new.instanceme thod, pickle_instance method,
    make_instanceme thod)

    if __name__ == "__main__":
        import pickle

        class A(object):
            def __init__(self, who):
                self.who = who
            def alpha(self):
                print "Hello,", self.who


        FILENAME = "method.pic kle"

        import sys
        args = sys.argv[1:]
        if args:
            a = A(args[0])
            m = a.alpha
            pickle.dump(m, open(FILENAME, "wb"))
        else:
            m = pickle.load(ope n(FILENAME, "rb"))
            m()

    $ python pickle_method.p y world
    $ python pickle_method.p y
    Hello, world
    $ python pickle_method.p y Srini
    $ python pickle_method.p y
    Hello, Srini

    Peter
    --



    Share files, take polls, and make new friends - all under one roof. Go to http://in.promos.yahoo.com/groups/
  • Peter Otten

    #2
    Re: How to pickle bound methods

    srinivasan srinivas wrote:

    Please don't top-post.
    Could you please explain the code breifly?? I am not getting what it does.
    >import copy_reg
    >import new
    >>
    >def make_instanceme thod(inst, methodname):
    >    return getattr(inst, methodname)
    >>
    >def pickle_instance method(method):
    >    return make_instanceme thod, (method.im_self , method.im_func. __name__)
    >>
    >copy_reg.pickl e(new.instancem ethod, pickle_instance method,
    >make_instancem ethod)
    If you have a type that cannot be pickled because it is implemented in C you
    can make it "picklable" by registering it with the copy_reg.pickle ()
    function. This function takes three arguments:

    1 the type (here: new.instancemet hod)
    2 a function that takes an instance of the type. This returns a factory
    function and a tuple of the arguments this factory function needs to
    recreate the instance.
    3 the factory function.

    In short the following must work, and out_method should do the same thing as
    in_method:

    factory, args = pickle_instance method(in_metho d)
    out_method = factory(*args)

    Now to your other problem, pickling static methods. The type of a static
    method is just new.function, the same as that of a global function. Global
    functions are already picklable, so the copy_reg mechanism doesn't kick in.

    Peter

    Comment

    Working...