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:
$ 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/
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??
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/
Comment