Pumpkin
Pumpkin
TTCTheo's Typesafe Cult
Created by Simbaclaws on 1/11/2025 in #questions
Python issue with inheritence
$ python python3.py
MyInheritedClass is a subclass of MySuperClass
instance is a subclass of MySuperClass
$ python python3.py
MyInheritedClass is a subclass of MySuperClass
instance is a subclass of MySuperClass
4 replies
TTCTheo's Typesafe Cult
Created by Simbaclaws on 1/11/2025 in #questions
Python issue with inheritence
inheritedClass is a reference to the class itself, not an instance of the class. Hence isinstance is False, because it is just an instance of type.
import importlib
from python1 import MySuperClass

module = importlib.import_module('python2')
inheritedClass = getattr(module, 'MyInheritedClass')
if issubclass(inheritedClass, MySuperClass):
print("MyInheritedClass is a subclass of MySuperClass")
else:
print("MyInheritedClass isn't a subclass of MySuperClass")

instance = inheritedClass()
if isinstance(instance, MySuperClass):
print("instance is a subclass of MySuperClass")
else:
print("instance isn't a subclass of MySuperClass")
import importlib
from python1 import MySuperClass

module = importlib.import_module('python2')
inheritedClass = getattr(module, 'MyInheritedClass')
if issubclass(inheritedClass, MySuperClass):
print("MyInheritedClass is a subclass of MySuperClass")
else:
print("MyInheritedClass isn't a subclass of MySuperClass")

instance = inheritedClass()
if isinstance(instance, MySuperClass):
print("instance is a subclass of MySuperClass")
else:
print("instance isn't a subclass of MySuperClass")
4 replies