2

I want to check if a passed function is a Numpy function or not. for example:

import numpy as np

def my_func(passed_func):
    if passed_func is Numpy function: # Here is the problem
        print('It is')
    else:
        print('It is not')

>>>> my_func(np.random.randn)
>>>> It is

>>>> my_func(np.random.randint)
>>>> It is

>>>> my_func(lambda x : x+2)
>>>> It is not

Thanks in advance :)

A.Najafi
  • 691
  • 1
  • 9
  • 20

2 Answers2

3

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)
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
-2

How about using hasattr function?

import numpy as np
def my_func(passed_func):
    if hasattr(np, passed_func):
        print('It is')
    else:
        print('It is not')

my_func("array")
my_func("list") 
LevB
  • 925
  • 6
  • 10
  • attribute name must be string so ```hasattr(np, passed_func.__name__): ``` but again it does not work – A.Najafi May 10 '20 at 09:35