Friday, December 27, 2024
Google search engine
HomeLanguagesHow to detect whether a Python variable is a function?

How to detect whether a Python variable is a function?

There are times when we would like to check whether a Python variable is a function or not. This may not seem that much useful when the code is of thousand lines and you are not the writer of it one may easily stuck with the question of whether a variable is a function or not. We will be using the below methods to check the same.

  • By calling the built-in function callable()
  • By importing isfunction() from the inspect module
  • By using the type() function
  • By calling the built-in function hasattr() function
  • By using the isinstance() function

Detect whether a Python variable is a function or not by using a callable() function

It is a function that returns the boolean value True if the function is callable otherwise it returns false. And Syntax of the callable function.

Python3




def subtract(a, b):
    return a-b
 
print(callable(subtract))
value = 15
print(callable(value))


Output:

True
False

Detect whether a Python variable is a function or not by using the inspect module 

Inspect is a module from which we will have to use the isfunction which returns a boolean value True if it is a function else return false.  And for using this, First, you have to import isfunction from inspect, and after that use, isfunction to get the boolean value.

Python3




from inspect import isfunction
 
def subtract(a, b):
    return a-b
 
print(isfunction(subtract))
val = 10
print(isfunction(val))


Output:

True
False

Detect whether a Python variable is a function or not by using the type() function

It is a function that tells us the type of an object by which we will check if the type of object is a function then it is callable otherwise it is not callable.

Python3




def subtract(a, b):
    return a-b
 
print(type(subtract))
value = 'GFG'
print(type(value))


Output:

<class 'function'>
<class 'str'>

Detect whether a Python variable is a function or not by using the hasattr() function

hasattr() is a function that tells us the type of an object by which we will check if the type of object is a function or not. It returns a boolean value as well just like callable().

Python3




def subtract(a, b):
    return a-b
 
 
print(hasattr(subtract, '__call__'))
value = 'GFG'
print(hasattr(value, '__call__'))


Output:

True
False

 

isinstance() is a function that tells us the type of object by which we will check if the type of object is a function or not. It returns a boolean value as well just like hasattr().

Python3




import types
def subtract(a, b):
    return a-b
 
print(isinstance(subtract, types.FunctionType))
value = 'GFG'
print(isinstance(value, types.FunctionType))


Output:

True
False

Detect whether a Python variable is a function or not by using inspect module:

One alternative approach to detect whether a Python variable is a function is to use the inspect module. This can be done by importing the isfunction method from inspect and then calling isfunction on the variable in question. For example:

Python3




import inspect
 
def test_function():
    pass
 
print(inspect.isfunction(test_function))  # Output: True
 
value = 10
print(inspect.isfunction(value))  # Output: False


Output:

True
False

RELATED ARTICLES

Most Popular

Recent Comments