An object is and isn't an instance of a class at the same time

danielk danielkleinad at
Mon Dec 10 00:34:39 CET 2012

I was debugging some code using isinstance() to make sure the correct object was been passed to the method and came across something that is really ticking me off. 

I have a class called 'Jitem' in its own file called ''. It's part of a package called 'jukebox'. I also have '__all__' that includes 'jitem' so that I can do:

from jukebox import *

There is another class that has a method that does this (simplified for this example):

def execute(self, command):

I stuck this debug code in the method:

if not isinstance(command, jitem.Jitem):
    raise TypeError("Command must be an instance of Jitem.")

When this method gets run in a test script, it returns this:

<class 'jukebox.jitem.Jitem'>
Traceback (most recent call last):
  File "", line 4, in <module>
    executeResults = jc.execute(cmnd)
  File "D:\home\python\jukebox\", line 225, in execute
    raise TypeError("Command must be an instance of Jitem.")
TypeError: Command must be an instance of Jitem.

How can it both get past isinstance() and still say it is the proper class?

Dan Klein

More information about the Python-list mailing list