1

Your any help would be great. Thanks in advance!

I have 2 files, like below

file1:

import file2

class Foo():
    pass

if __name__ == '__main__':
    foo = file2.dummy()
    print("right?", isinstance(foo, Foo))
    print("right?", type(foo)==Foo)

file2:

import file1

def dummy():
    foo = file1.Foo()
    return foo

if __name__ == '__main__':
    pass

Actually, I expect that isinstance(foo, Foo) is True, type(foo) == Foo is also True, but I get two False. How do I get my expectations?

  • 2
    Because you have circular imports, I believe file2 re-loads file1 as a distinct module object. I feel there is a duplicate somewhere. – juanpa.arrivillaga Apr 25 '18 at 09:31
  • @juanpa.arrivillaga Thankes. Yeah, I know that. But how could I fix it? – fanronghong Apr 25 '18 at 09:33
  • Don't use a circular import. Don't use your module as a script. Have a *seperate* file that imports both file1 and file2 – juanpa.arrivillaga Apr 25 '18 at 09:33
  • You have two instances of the `file1` module - one registered under the name `file1`, and the other under the name `__main__`. The solution is to separate your library code from executable code. Move your `if __name__ == '__main__':` block into another file. – Aran-Fey Apr 25 '18 at 09:34
  • 1
    Getting rid of the circular import would be a good idea as well, but it's not strictly necessary. – Aran-Fey Apr 25 '18 at 09:36

0 Answers0