I am trying to import a class from one file and check if is an instance of that class in the file that it was defined in. The problem is that instead of returning True
from theisinstance()
function, it returns False
, because it was initialised in a different file.
Here is a working example.
Say you have file1.py
:
class Foo:
def __init__(self, arg1):
self.arg1 = arg1
def main(class_obj):
# Prints false and is type <class 'file1.Foo'>
print(type(class_obj))
print(isinstance(class_obj, Foo))
if __name__ == '__main__':
from file2 import get_class
main(get_class())
And file2.py
:
from file1 import Foo
def get_class():
foo = Foo("argument")
return foo
It prints False
and the type is <class 'file1.Foo'>
. What I found interesting is that if you initialize the Foo
class in file1
where is was defined, it returns True
.
# Add to main() function in file1
# Returns true and is type <class '__main__.Foo'>
foo_local = Foo("argument") # Class initiated in __main__ seems to work
print(type(foo_local))
print(isinstance(foo_local, Foo))
I found that if you initiate a class outside of the file where it is defined, it is a different "class" than if you initiate the class inside the file where it was defined.
# Different Classes?
<class 'file1.Foo'> # From other file (`file2.py`)
<class '__main__.Foo'> # From same file (`file1.py`)
So my question is:
How can I work around this so that even the classes initiated outside of the file1
can return True
on the isinstance()
function? To reword it, how can I make it so that the Foo
class is the "same" in file1.py
and file2.py
? I Python 3.6.7 if it matters.