Hot-updating-python-libraries/Self_alter.py
2026-02-06 14:19:32 -05:00

24 lines
No EOL
760 B
Python

import lib_module
def update():
import importlib
importlib.reload(lib_module)
del importlib
def change_my_lib():
with open("lib_module.py", "w") as libby:
libby.write("def func_in_lib():\n\tprint('new logic!')\n")
def reset_project_demo():
with open("lib_module.py", "w") as libby:
libby.write("def func_in_lib():\n\tprint('Old logic :(')\n")
if __name__ == '__main__':
lib_module.func_in_lib()
change_my_lib()
lib_module.func_in_lib()
# Notice the logic doesn't change till we update it. '
update()
lib_module.func_in_lib()
# Look, now we performed a hot reload of a library without turning off the program
# It should be obvious where this can be useful.
reset_project_demo()