Here's a contrived toy example for triggering a problem I'm having:
I have several classes, assume they're in a local file 'issue.py':
class A(object):
def save(self):
# fancy stuff
pass
class B(A):
def save(self):
# misc stuff
super(B, self).save()
class C(B):
pass
I use them in an IPython session, perhaps like so:
In [1]: %load_ext autoreload
In [2]: %autoreload 2
In [3]: from issue import A, B, C
In [4]: c = C()
In [5]: c.foo = 'whatever'
In [6]: c.save()
So far, so good. But then I realize there's a bug in the class A 'fancy-stuff', and make a small edit there – maybe even just adding a bit of logging. I then want to repeat the save()
:
In [7]: c.save()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-6970514bfc33> in <module>()
----> 1 c.save()
/Users/scratch/Documents/dev2015/gensim_venv/src/gensim-develop/docs/notebooks/scratch~/issue.py in save(self)
7 def save(self):
8 # misc stuff
----> 9 super(B, self).save()
10
11 class C(B):
TypeError: super(type, obj): obj must be an instance or subtype of type
Oh no!. The dreaded TypeError
after reloading classes, while older instances retain some older superclasses! There is discussion of this issue at SO and elsewhere, but no clear recipe for recovery.
But as it so happens, I would really, really like to be able to run the ever-so-slightly updated A.save()
on my old c
instance. (I've got 20GB+ of data in memory that took about a day and half to generate that would be saved-in-the-preferred-manner by the superclass method. I've saved enough aside via other manual methods that I think I'd be able to reconstruct c
in a restarted IPython kernel. But, while I've still got the authentic object around, I'd much prefer to give a realistic test to the patched A.save()
– maybe even making more fixes/tests to it before a full kernel restart.)
So I'm interested in any tactics or tricks, no matter how unwise they might be in other situations, for coercing c
into the current class definitions, all the way up, so that c.save()
just works.
Any ideas?
I expect anything that works for this toy example will work in my real setup, which is a CPython 2.7.10-based IPython). (However, in the real situation, the three classes are in different files.)