exec, import and isinstance

Michele Petrazzo michele.petrazzo at REMOVE_me_unipex.it
Tue Feb 8 03:57:35 EST 2011


Hi all,
I'm playing with python internals and I'm discovering a strange behavior
of isinstance. Considering the following test case:

test/bar.py
test/b.py
test/a/__init__.py
test/a/foo.py

-- __init__.py -> empty

--- foo.py:
class foo: pass
c = foo

--- b.py
def ret():
   d = {}
   #s = "import sys;sys.path.append('a');import foo" ## this cause the 
problem
   s = "import sys;from a import foo"
   exec s in d
   return d["foo"].c

--- bar.py
import a.foo
import b

c1 = a.foo.foo
c2 = b.ret()

print c1, c2
print isinstance(c2(), c1)
---

Executing bar.py, I receive:
a.foo.foo a.foo.foo
True

But, if I replace the indicated line, python doesn't recognize anymore
the instance and I receive:
a.foo.foo foo.foo
False

Why? I don't understand how python see the instance. Or
better, does it see only the path? The classes (and the instances) are 
the same!

Thanks for all that help me to turn the light on...

Michele



More information about the Python-list mailing list