isinstance() not recognizing an instance correctly (python bug?)

Martin v. Loewis martin at v.loewis.de
Wed May 8 01:41:24 EDT 2002


Fernando Perez <fperez528 at yahoo.com> writes:

> I've found a rather unpleasant situation where isinstance(obj,cls)==0, even 
> though obj _is_ indeed a cls() instance. I don't know if this should be 
> considered a bug or just an unavoidable consequence of the circumstances 
> where it happens.

That's really hard to believe. Can you demonstrate it in a small,
self-contained example (including instructions like what Python
version to use, on what operating system, and what commands to execute
in what order)?

> Well,  when 's' above is passed to display(), it fails to identify that s is 
> indeed an instance of someclass(). The reason, I think, is that the 
> interactive code is executed in a separate namespace than the display code, 
> and therefore someclass exists with two different memory addresses. 

This is very unlikely. When somemodule is imported the second time,
sys.modules is checked to see whether it is already imported, and if
so, the existing module is re-used. In turn, somemodule.someclass
ought to be the same module all the time.

One possible cause is that you import the same code both as a
submodule of a package, and as a top-level module
(i.e. somepackage.somemodule vs. somemodule). In that case, those two
copies will indeed have distinct definitions of someclass.

In any case, without seeing further details, I'm unwilling to believe
that what you report actually happened.

Regards,
Martin




More information about the Python-list mailing list