By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,588 Members | 1,084 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,588 IT Pros & Developers. It's quick & easy.

How to pickle bound methods

P: n/a
I would like*to know how to pickle a bound method??
Meet people who discuss and share your passions. Go to
Jul 2 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
srinivasan srinivas wrote:
I would likeĀ*to know how to pickle a bound method??
$ cat
import copy_reg
import new

def make_instancemethod(inst, methodname):
return getattr(inst, methodname)

def pickle_instancemethod(method):
return make_instancemethod, (method.im_self, method.im_func.__name__)

copy_reg.pickle(new.instancemethod, pickle_instancemethod,

if __name__ == "__main__":
import pickle

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

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

$ python world
$ python
Hello, world
$ python Srini
$ python
Hello, Srini

Jul 2 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.