Given a class or function, is there a way to find the full path of the module where it is originally defined? (I.e. using def xxx
or class xxx
.)
I'm aware that there is sys.modules[func.__module__]
. However, if func
is imported in a package's __init__.py
, then sys.modules
will simply redirect to that __init__.py
, because the function has been brought into that namespace, as far as my understanding goes.
A concrete example:
>>> import numpy as np >>> import sys >>> np.broadcast.__module__ 'numpy' >>> sys.modules[np.broadcast.__module__] <module 'numpy' from '/Users/brad/.../site-packages/numpy/__init__.py'>
Obviously, broadcast
is not defined in __init__.py
; it is just brought into the namespace with one of these from module import *
statements.
It would be nice to see where in the source np.broadcast
is defined (regardless of the file extension, be it .c or .py). Is this possible?