[ python-Bugs-965562 ] isinstance fails to recognize instance

SourceForge.net noreply at sourceforge.net
Thu Jun 3 09:40:37 EDT 2004


Bugs item #965562, was opened at 2004-06-03 09:55
Message generated for change (Comment added) made by doerwalter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=965562&group_id=5470

Category: Python Interpreter Core
Group: Python 2.2.3
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Ib Jørgensen (ijorgensen)
Assigned to: Nobody/Anonymous (nobody)
Summary: isinstance fails to recognize instance

Initial Comment:
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.


----------------------------------------------------------------------

>Comment By: Walter Dörwald (doerwalter)
Date: 2004-06-03 15:40

Message:
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.

----------------------------------------------------------------------

Comment By: Ib Jørgensen (ijorgensen)
Date: 2004-06-03 13:23

Message:
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.


----------------------------------------------------------------------

Comment By: Walter Dörwald (doerwalter)
Date: 2004-06-03 12:43

Message:
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?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=965562&group_id=5470



More information about the Python-bugs-list mailing list