I want to modify a module xyz and its functions like that:
def modify(fun): modulename = fun.__module__ # this is string. ok, but not enough import xyz
Use the inspect module:
import inspect def modify(fun): module = inspect.getmodule(fun)
This is the same as polling the module from sys.modules using fun.__module__. Although getmodule tries harder even if fun does not have a __module__ attribute.
sys.modules
fun.__module__
getmodule
fun
__module__