[Tutor] TypeError in base class __init__ after reload

Daniel Knierim tknierim at mail.ieway.com
Thu Jan 24 04:48:27 CET 2008


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)

on the last call to ChildClass(), at line 23. The earlier calls don't trigger the error.

-- Begin file instantiate_and_reload.py:
from parentclass import ParentClass
from childclass import ChildClass
import parentclass, childclass


p = parentclass.ParentClass()
c = childclass.ChildClass()
p = ParentClass()
c = ChildClass()

reload(parentclass)

p = parentclass.ParentClass()
c = childclass.ChildClass()
p = ParentClass()
c = ChildClass()

reload(childclass)

p = parentclass.ParentClass()
c = childclass.ChildClass()
p = ParentClass()
c = ChildClass()

-- End of file instantiate_and_reload.py

-- Begin file parentclass.py:
class ParentClass(): #same result with new-style classes (subclassing object)
    def __init__(self):
        pass


if __name__ == '__main__':
    p = ParentClass()

-- End of file parentclass.py

-- Begin file childclass.py:
from parentclass import ParentClass
import parentclass


class ChildClass(ParentClass):
    def __init__(self):
        ParentClass.__init__(self)


if __name__ == '__main__':
    p = ParentClass()
    c = ChildClass()

    reload(parentclass)
    
    p = ParentClass()
    c = ChildClass()

-- End of file childclass.py

-- Beginning of captured command-line session:
D:\Programs\Python\Sandbox\SubclassInitChain>python childclass.py

D:\Programs\Python\Sandbox\SubclassInitChain>python parentclass.py

D:\Programs\Python\Sandbox\SubclassInitChain>python instantiate_and_reload.py
Traceback (most recent call last):
  File "instantiate_and_reload.py", line 23, in <module>
    c = ChildClass()
  File "D:\Programs\Python\Sandbox\SubclassInitChain\childclass.py", line 7, in __init__
    ParentClass.__init__(self)
TypeError: unbound method __init__() must be called with ParentClass instance as first argument (got ChildClass instance instead)

D:\Programs\Python\Sandbox\SubclassInitChain>python -V
Python 2.5.1

-- end of captured command-line session

I can't understand why the last call to ChildClass() triggers the error, when the earlier calls don't.

Can somebody explain to me what's going on?  or what I'm doing wrong?

Thanks
- Dan Knierim


More information about the Tutor mailing list