I have a Python package with dozens of subpackages/modules. Nearly each of the modules uses open
built-in Python function. I have written an own implementation of the file opening function and would like to "redirect" all open
calls I have in the package modules to my_open
function.
I am aware that it is possible to write open = my_open_file
in the top of the module to shadow the open
within the module, but this would imply editing each module. Alternatively, putting open = my_open_file
in the __init__.py
of the package and then doing from package_name import open
which also implies adding a single line of code to each module.
Is it possible to define the my_open_file
function for the package scope just in a single place? Or adding a single line of code in each module is my only option?