zkhd_w
Last Updated: February 25, 2016
·
1.046K
· maandree

Simple extensible Python code

Python supports dynamically loading python files and executing them.
A simple example on how to do this is:

addon = SOME_PYTHON_FILE
code = None
with open(addon, 'r') as file:
    code = file.read() + '\n'
code = compile(code, addon, 'exec')
exec(code, globals())

And Python also supports defining methods:

class Class:
     def something(self):
          print(self.variable)


def new_something(self):
    print(self.other_variable)

Class.something = new_something.

But you can also union functions by storing them in a variable before replacing them and calling those variables inside the replacement functions.

This makes it simple to create extensible Python programs by just having executing an rc file that can load the addons and just replace and union functions. And to make the code more extensible, just keep in mind to have small functions.