I have an issue where I need to instantiate a class, but I'm not sure what the most optimal way to go about this is.
Imagine the following code:
# loadstuff.py
class LoadSomething:
def load(self, filename):
compiled = compile(open(filename).read(), filename, 'exec')
# The part I am stuck on
# How do I get the class name, and how do I instantiate it?
# This is assuming the file name is the same as the class name, only starting with capital letter, and minus file extension
module = compiled. # instantiate class 'Printstuff'
return module
loader = LoadSomething()
module = loader.load("printstuff.py")
module.printSomething() # Should print "Something!"
# printstuff.py
class Printstuff:
def printSomething(self):
print("Something!")
I think the code (and thus the question) speaks mostly for itself, how do I return and instantiate a new class with compile(..)
so that I can call its methods?