This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: isinstance fails to recognize instance
Type: Stage:
Components: Interpreter Core Versions: Python 2.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: doerwalter, ijorgensen
Priority: normal Keywords:

Created on 2004-06-03 07:55 by ijorgensen, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg21005 - (view) Author: Ib J�rgensen (ijorgensen) Date: 2004-06-03 07:55
I searched the bugbase and found a somewhat similar 
problem that was termed invalid (id 563730 )
I did however spend some time figuring out what was 
going wrong so..?

The following two modules document the problem:
MODULE A:
import b

class A:
    def __init__(self):
        self.name = "a"


if __name__ == "__main__":
    x = A()
    print "main",isinstance(x,A)
    y = b.B()
    y.notify(x)

MODULE B:
import a
class B:
    def __init__(self):
        self.name = "b"

    def notify(self,cl):
        print "notify",isinstance(cl,a.A)



if __name__ == "__main__":
    x = a.A()
    print isinstance(x,a.A)
    y = B()
    y.notify(x)

running module A gives inconsistent results
the printout is:
main True
notify False

The modules do have circular imports and it is clear that 
this is behind the problem - but that may happen in real 
life.
msg21006 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2004-06-03 10:43
Logged In: YES 
user_id=89016

The problem is not the circular import per se, but the fact 
that module a gets imported twice, once as the __main__ 
module (with it's version of the class A) and once as the 
module A (with it's own version of the class A).

From Python's point of view these are two completely 
unrelated classes with on inheritance relationship whatsoever.

Can this bug be closed?
msg21007 - (view) Author: Ib J�rgensen (ijorgensen) Date: 2004-06-03 11:23
Logged In: YES 
user_id=1055299

I have no problems if you close this bug. I do however feel 
that it is quite subtle to distinguish classes by the rather 
random import order.
msg21008 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2004-06-03 13:40
Logged In: YES 
user_id=89016

It has nothing to do with the *order* of the imports. You can 
get this problem even if you have only one module. Write a 
module foo.py with the following content:
--
class Foo:
   pass

import foo
print isinstance(foo.Foo(), Foo)
--
When you run this script with "python foo.py", it will print
True
False
The first output is from the "import foo" statement, which 
imports the module under the name foo. The second output 
will come from the __main__ script.

The consequence of all this is: Don't import the __main__ 
script.

Closing the bug report.
History
Date User Action Args
2022-04-11 14:56:04adminsetgithub: 40338
2004-06-03 07:55:37ijorgensencreate