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

Chris Angelico rosuav at
Mon Dec 10 01:31:13 CET 2012

On Mon, Dec 10, 2012 at 10:34 AM, danielk <danielkleinad at> wrote:
> D:\home\python>python
> <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?

You're invoking it as __main__ and then also importing it. This gives
you two instances of your module, with two separate classes that have
the same name and (presumably) the same definition.

If you use a separate driver script, you won't see this problem.
Alternatively, simply stop checking isinstance and trust that, if
something incompatible gets passed in, it'll throw an exception
somewhere. That's usually enough.


More information about the Python-list mailing list