Theo's Typesafe CultTTC
Theo's Typesafe Cult13mo ago
3 replies
Simbaclaws

Python issue with inheritence

I'm trying to check if a inherited class is an instance of it's superclass. But the inherited class and the super class are in separate files.

I'm importing the inherited class with importlib as a dynamic import.

So for example:
python1.py:
class MySuperClass:
  pass

python2.py:
from python1 import MySuperClass

class MyInheritedClass(MySuperClass):
  pass

python3.py:
import importlib
from python1 import MySuperClass

module = importlib.import_module('python2')
inheritedClass = getattr(module, 'MyInheritedClass')
if isinstance(inheritedClass, MySuperClass):
  print("MyInheritedClass is instance of MySuperClass")
else:
  print("MyInheritedClass isn't an instance of MySuperClass")


This prints: MyInheritedClass isn't an instance of MySuperClass

Does anyone know why that is the case?
Was this page helpful?