S
Steven Bethard
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 again later.
Sometimes the callable ``foo`` is actually a bound method, e.g.
``bar.baz``, but in such cases, pickle doesn't work by default because
it doesn't know how to pickle/unpickle instancemethod objects.
I was thinking of doing something like::
.... func_name = method.im_func.__name__
.... cls = method.im_class
.... obj = method.im_self
.... return unpickle_instancemethod, (func_name, cls, obj)
........ if obj is None:
.... return getattr(cls, func_name)
.... else:
.... return getattr(obj, func_name)
........ def __init__(self, foo, bar):
.... self.foo = foo
.... self.bar = bar
.... def baz(self):
.... return self.foo, self.bar
........ pickle_instancemethod,
.... unpickle_instancemethod)
This seems to basically do the right thing on the few simple things I've
tried::
('eki', 'fekang')
Does this approach seem sound? Am I going to run into some weird
problems doing it this way?
Thanks,
STeVe
want to be able to delay a call like ``foo(spam, badger)`` by dumping
``foo``, ``spam`` and ``badger`` to disk and loading them again later.
Sometimes the callable ``foo`` is actually a bound method, e.g.
``bar.baz``, but in such cases, pickle doesn't work by default because
it doesn't know how to pickle/unpickle instancemethod objects.
I was thinking of doing something like::
.... func_name = method.im_func.__name__
.... cls = method.im_class
.... obj = method.im_self
.... return unpickle_instancemethod, (func_name, cls, obj)
........ if obj is None:
.... return getattr(cls, func_name)
.... else:
.... return getattr(obj, func_name)
........ def __init__(self, foo, bar):
.... self.foo = foo
.... self.bar = bar
.... def baz(self):
.... return self.foo, self.bar
........ pickle_instancemethod,
.... unpickle_instancemethod)
This seems to basically do the right thing on the few simple things I've
tried::
('eki', 'fekang')
Does this approach seem sound? Am I going to run into some weird
problems doing it this way?
Thanks,
STeVe