Reloading exception explained (Re: mutlifile inheritance problem simplified)

Greg Ewing greg at cosc.canterbury.ac.nz
Sun Mar 24 21:35:51 EST 2002


Marc wrote:
> 
> "D:\PythonCode\pna\eyeTracking\tests\b01.py", line
> 8, in __init__
>     a01.a.__init__(self)
> TypeError: unbound method must be called with
> class instance 1st argument

I can see what's happening here. It's because
you're reloading some modules more than once.
When you import b01, you reload a01, and then
when you import c01, you relod a01 *again*.

The result of this is that there are *two*
classes "a" in existence. Class b inherits from
the first one, created when you reloaded a01 for
the first time, and class c inherits from the
second one, created when you reloaded a01 for
the second time.

Now, when you come to do

   a01.a.__init__(self)

in class b, you're dealing with an instance which
has the *first* incarnation of class a as a superclass.
But a01.a is bound to the *second* incarnation of 
class a, so when Python checks whether self is an
instance of a01.a, it finds that it's not, and
throws an exception.

The moral of this is, if you must use reload(),
make sure that you reload each module only once,
and in the correct order.

Also be aware of all the other limitations
of reload(), such as the fact that any instances
you may have around from before the reload will
still be instances of the old versions of your
classes.

Personally I think it's better to avoid using
reload() at all, because of all its limitations.
Arranging for your computation to be restartable
in some other way, as was suggested elsewhere in
this thread, is a much better idea.

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list