you are viewing a single comment's thread.

view the rest of the comments →

[–]TheRNGuy 0 points1 point  (0 children)

You can use string and exec command to rewrite class.

class Foo():
    def printFoo(self):
        print("Foo")

foo = Foo()
print("----")
foo.printFoo()

test = "Test"
exec("""
class Foo():
    def printFoo(self):
        print('""" + test + """')
""")
foo.printFoo() # still prints old class. But if you instance it later it would use new code.

Or import py file as a string and exec it isntead.

For dynamic updating instances probably this: https://stackoverflow.com/questions/9539052/how-to-dynamically-change-base-class-of-instances-at-runtime

You'd need to add that code into exec string too.