I have a class (Bar
) which effectively has its own state and callback(s) and is used by another class (Foo
):
class Foo(object):
def __init__(self):
self._bar = Bar(self.say, 10)
self._bar.work()
def say(self, msg):
print msg
class Bar(object):
def __init__(self, callback, value):
self._callback = callback
self._value = value
self._more = { 'foo' : 1, 'bar': 3, 'baz': 'fubar'}
def work(self):
# Do some work
self._more['foo'] = 5
self._value = 10
self._callback('FooBarBaz')
Foo()
Obviously I can't pickle the class Foo
since Bar
has an instancemethod, so I'm left with the following solution of implementing __getstate__
& __setstate__
in Bar
to save self._value
& self._more
, but I have to instantiate the self._callback
method as well (i.e. call __init__()
from the outer class Foo
passing the callback function.
But I cannot figure out how to achieve this.
Any help is much appreciated.
Thanks.