You get the name of a function with .__name__
and can inspect a module using dir - but your functions are part of the np.random module.
You can try to get to it like this:
import numpy as np
def my_func(passed_func):
n = passed_func.__name__ # get the name of the thing thats being passed
for part in [np, np.random]: # list or fetch somehow all np-modules of interest
# check the name in the module of interests
if n in dir(part):
return f"{n} is part of {part.__name__}"
# fail
return f"{n} is not in numpy"
print(my_func(np.random.randn))
print(my_func(np.random.randint))
print(my_func(lambda x : x+2))
Output:
randn is part of numpy.random
randint is part of numpy.random
<lambda> is not in numpy
Look into List all the modules that are part of a python package? to list all numpy pkg. It's answer transformed to Python 3 gives you:
import numpy as np
import pkgutil
for importer, modname, ispkg in pkgutil.iter_modules(np.__path__):
print (f"Found submodule {modname} (is a package: {ispkg})")
Output:
Found submodule __config__ (is a package: False)
Found submodule _distributor_init (is a package: False)
Found submodule _globals (is a package: False)
Found submodule _pytesttester (is a package: False)
Found submodule compat (is a package: True)
Found submodule conftest (is a package: False)
Found submodule core (is a package: True)
Found submodule ctypeslib (is a package: False)
Found submodule distutils (is a package: True)
Found submodule doc (is a package: True)
Found submodule dual (is a package: False)
Found submodule f2py (is a package: True)
Found submodule fft (is a package: True)
Found submodule lib (is a package: True)
Found submodule linalg (is a package: True)
Found submodule ma (is a package: True)
Found submodule matlib (is a package: False)
Found submodule matrixlib (is a package: True)
Found submodule polynomial (is a package: True)
Found submodule random (is a package: True)
Found submodule setup (is a package: False)
Found submodule testing (is a package: True)
Found submodule tests (is a package: True)
Found submodule version (is a package: False)