473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Re: How to pickle bound methods

No. It does not work.

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

----- Original Message ----
From: Peter Otten <__*******@web.de>
To: py*********@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_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,
make_instancemethod)
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.instancemethod)
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_instancemethod(in_method)
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
--
http://mail.python.org/mailman/listinfo/python-list
Unlimited freedom, unlimited storage. Get it now, on http://help.yahoo.com/l/in/yahoo/mai...tools-08.html/
Jul 3 '08 #1
1 2790
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

Jul 3 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christian Tismer | last post by:
Martin v. Löwis wrote: > "Mark Hahn" <mark@hahnca.com> writes: > > >>I don't understand how this could happen with pickle. Isn't it supposed to >>stop when it runs into an object it has...
2
by: Jane Austine | last post by:
Hi. class A: def __init__(self,tick): if tick: self.foo=self.bar else: self.foo=self.bur def bar(self): print 'bar'
2
by: Steven Bethard | last post by:
I'd like to be able to pickle instancemethod objects mainly because I want to be able to delay a call like ``foo(spam, badger)`` by dumping ``foo``, ``spam`` and ``badger`` to disk and loading them...
5
by: Chris | last post by:
Why can pickle serialize references to functions, but not methods? Pickling a function serializes the function name, but pickling a staticmethod, classmethod, or instancemethod generates an...
2
by: Victor Lin | last post by:
Hi, I encounter a problem with pickle. I download a html from: ...
4
by: PBlitz | last post by:
I want to save the content of a custom made OrderedDict. (I got the OrderedDict from http://aspn.activestate.com/ASPN/Python) The pickle modul seemed to be the solution for the job. At first I...
2
by: Michele Simionato | last post by:
Can somebody explain what's happening with the following script? $ echo example.py import pickle class Example(object): def __init__(self, obj, registry): self._obj = obj self._registry =...
1
by: srinivasan srinivas | last post by:
Hi, I would like to know how to pickle a bound method?? Thanks, Srini Meet people who discuss and share your passions. Go to http://in.promos.yahoo.com/groups/bestofyahoo/
1
by: srinivasan srinivas | last post by:
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 ----...
10
by: est | last post by:
>>import md5 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python25\lib\pickle.py", line 1366, in dumps Pickler(file, protocol).dump(obj) File...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.