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

Justin Shaw wyojustin at hotmail.com
Sat Jul 20 01:28:00 EDT 2002


Hmmm,
I can't repeat your error:
# module1.py
class Super:
    def __init__(self):
        self.a = 'string1'

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

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

# junk.py
import module2
mid = module2.Mid()
print 'module2.Mid().a', mid.a
sub = module2.Sub()
print 'module2.Sub().a', mid.a

<dos command line>
c:\Python\Python22> junk.py
module2.Mid().a string1
module2.Sub().a string1

Did you remember to "import moudle1" in module2?

Justin

"David" <dlashar at sprynet.com> wrote in message
news:ah7ii3$atg$1 at slb3.atl.mindspring.net...
> 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