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

    No. It does not work.

    def make_staticmeth od(inst, methodname):
        return getattr(inst, methodname)
    def pickle_function (method):
        return make_staticmeth od, (method.im_self , method.im_func. __name__)
    copy_reg.pickle (new.function, pickle_function , make_staticmeth od)



    ----- Original Message ----
    From: Peter Otten <__peter__@web. de>
    To: python-list@python.org
    Sent: Thursday, 3 July, 2008 12:13:45 PM
    Subject: 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 aninstance of the type. This returns a factory
    function and a tuple of thearguments 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
    --



    Unlimited freedom, unlimited storage. Get it now, on http://help.yahoo.com/l/in/yahoo/mai...tools-08.html/
  • Peter Otten

    #2
    Re: How to pickle bound methods

    srinivasan srinivas wrote:

    Please don't top-post.
    No. It does not work.
    That's what I said. Try

    class A(object):
    @staticmethod
    def alpha():
    print "Hello"

    alpha = A.alpha

    Peter





    Comment

    Working...