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

    HI Peter,
    It works will for instance and class methods. But it doesn't work for static methods.
    Can you tel me how to pickle static methods as well??
    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

    srinivasansrini vas 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):
        return make_instanceme thod, (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
    --



    Bring your gang together. Do your thing. Find your favourite Yahoo! group at http://in.promos.yahoo.com/groups/
  • Peter Otten

    #2
    Re: How to pickle bound methods

    srinivasan srinivas wrote:

    (Please don't top-post)
    It works will for instance and class methods. But it doesn't work for
    static methods. Can you tel me how to pickle static methods as well??
    For static methods pickling is not so easy because these don't carry
    information about the class they belong to. What are you trying to do?

    Peter

    Comment

    Working...