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

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


On Mon, Dec 10, 2012 at 10:34 AM, danielk <danielkleinad at gmail.com> wrote:
> D:\home\python>python jtest.py
> <class 'jukebox.jitem.Jitem'>
> Traceback (most recent call last):
>   File "jtest.py", line 4, in <module>
>     executeResults = jc.execute(cmnd)
>   File "D:\home\python\jukebox\jconnection.py", 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.

ChrisA



More information about the Python-list mailing list