This question is about types comparisons, not python objects. More precisely, why a type defined in __main__
module is different from the one imported by importlib.import_module()
. Note that the problem doesn't happen (test2()
) if all types are imported by importlib.import_module()
.
Given a module called m.py:
class A(object):
pass
class B(A):
pass
def test1():
# pass
assert issubclass(B, A)
# prepare test with imported module
from importlib import import_module
m = import_module('m')
# pass
assert issubclass(m.B, m.A)
# fail
assert issubclass(m.B, A)
# fail
assert m.A is A
assert m.B is B
def test2():
from importlib import import_module
m0 = import_module('m')
m1 = import_module('m')
assert m0.A is m1.A
assert m0.B is m1.B
assert issubclass(m0.B, m1.A)
if __name__ == '__main__':
import sys
if len(sys.argv) == 2:
if sys.argv[1] == 'test1':
test1()
if sys.argv[1] == 'test2':
test2()
Why python m.py test1 fails and why python m.py test2 doesn't? (tested with py2.7.x and py3.4.x)