TypeError: unbound method...expected one instance, got another

David dlashar at sprynet.com
Thu Jul 18 19:21:27 EDT 2002


I have created a hierarchy of classes, and I am receiving an error that I
have not been able to figure out.  The error message (in Python 2.2) is:
"TypeError: unbound method __init__() must be called with Super instance as
first argument (got Sub instance instead)".

My situation is that I have three classes in a hierarchy, with the Super
living in a different module than the Mid and the Sub, as follows:

module1.class Super:
                   def __init__(self):
                        self.a = 'string1'

module2.class Mid(module1.Super):
                   def __init__(self):
                        self.b = 'string2'
                        module1.Super.__init__(self)

module2.class Sub(Mid):
                   def __init__(self):
                        self.c = 'string3'
                        Mid.__init__(self)

The curious thing to me (a newbie) is that sometimes instantiation of Sub()
works and sometimes it doesn't, depending on how I attempt it.  If I am at
the interactive prompt, instantiation works (except in a special case, which
I'll come to).  In other words, if I am at the interactive prompt, I am able
to create an instance of Sub() that includes the 'a' attribute from Super.
If, however, I try to create the instance in a script, I encounter the
TypeError when I run the script.  If, after encountering the TypeError, I
return to the interactive prompt and again try to create an instance of Sub,
I again encounter the TypError (this is the special case).   I continue to
get the error until such time as I reload module2, after which I am back
where I started, i.e., able to create an instance of Sub from the
interactive prompt but not from my script.

I feel like I'm misunderstanding something... but can't figure out what it
is.  Any insight or help would be appreciated.  Thanks,

    David





More information about the Python-list mailing list