[Tutor] TypeError in base class __init__ after reload

Kent Johnson kent37 at tds.net
Thu Jan 24 14:51:38 CET 2008

Oops, sending to the list with this PS:
Google 'python reload' for discussion and workarounds.

Daniel Knierim wrote:
> Hello Pythonistas,
> Running the script 'instantiate_and_reload.py' gives me the error 
>     TypeError: unbound method __init__() must be called with ParentClass instance as first argument (got ChildClass instance instead)

I haven't looked at your script closely enough to know exactly where the
  error is but there are some real problems with reloading modules when
you have references to objects in the module object. The module is
reloaded but the references are not automatically rebound to the new
module. In particular, names that are imported from the module will
still refer to the original module, and instances of classes from the
module will refer (via their __class__ attribute) to the original class.

The docs for reload() talk about this a little:

For example (using the cmd module just because it contains a class):
In [1]: from cmd import Cmd

Cmd is bound to the class object in the cmd module
In [2]: c=Cmd()

c is an instance of the original class

In [3]: import cmd
In [4]: cmd.Cmd is Cmd
Out[4]: True
In [5]: isinstance(c, cmd.Cmd)
Out[5]: True

cmd.Cmd, Cmd and c.__class__ all refer to the same class

In [6]: reload(cmd)
Out[6]: <module 'cmd' from
In [7]: isinstance(c, cmd.Cmd)
Out[7]: False

We now have a new class object bound to cmd.Cmd, it is not the same object!

In [8]: from cmd import Cmd
In [9]: c.__class__
Out[9]: <class cmd.Cmd at 0x10ac870>
In [10]: c.__class__ is Cmd
Out[10]: False

c.__class__ is still the old Cmd class

In [11]: Cmd is cmd.Cmd
Out[11]: True
In [12]: reload(cmd)
Out[12]: <module 'cmd' from
In [13]: Cmd is cmd.Cmd
Out[13]: False

Cmd itself has the same trouble.


More information about the Tutor mailing list