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.