Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to detect whether a Python variable is a function?
In this article, we will learn how to detect whether a Python variable is a function. Sometimes, it's essential to figure out whether a Python variable is a function, especially when working with large codebases or dynamically generated code.
Methods to Check if a Variable is a Function
The following are the methods to check whether a Python variable is a function ?
Using the built-in callable() function
Using the isfunction() of the inspect module
Using the type() function
Using the built-in hasattr() function
Using the isinstance() function
Using the callable() Function
The callable() function returns True if the object can be called like a function, else it returns False.
Syntax
callable(object)
Example
The following program checks whether a Python variable is a function using the built-in callable() function ?
# creating a function that returns the addition of 2 numbers
def addition(p, q):
return p + q
# using callable() to check if 'addition' is a function
print("Is 'addition' callable?", callable(addition))
# creating a variable with a number
number = 10
print("Is 'number' callable?", callable(number))
# testing with other callable objects
my_list = [1, 2, 3]
print("Is 'my_list' callable?", callable(my_list))
print("Is 'print' callable?", callable(print))
Is 'addition' callable? True Is 'number' callable? False Is 'my_list' callable? False Is 'print' callable? True
Using the inspect.isfunction() Method
The isfunction() function from the inspect module specifically checks if the variable is a user-defined function. It returns True only for functions, not for other callable objects like built-in functions or classes.
Example
from inspect import isfunction
# creating a user-defined function
def addition(p, q):
return p + q
# checking if variables are functions
print("Is 'addition' a function?", isfunction(addition))
number = 10
print("Is 'number' a function?", isfunction(number))
# testing with built-in functions
print("Is 'print' a function?", isfunction(print))
print("Is 'len' a function?", isfunction(len))
Is 'addition' a function? True Is 'number' a function? False Is 'print' a function? False Is 'len' a function? False
Using the type() Function
The type() function returns the exact type of an object. We can compare it with the function type to determine if it's a function.
Example
# creating a function
def addition(p, q):
return p + q
# checking the type of variables
print("Type of 'addition':", type(addition))
print("Is 'addition' a function?", type(addition).__name__ == 'function')
number = 10
print("Type of 'number':", type(number))
print("Is 'number' a function?", type(number).__name__ == 'function')
Type of 'addition': <class 'function'> Is 'addition' a function? True Type of 'number': <class 'int'> Is 'number' a function? False
Using the hasattr() Function
The hasattr() function checks if an object has the __call__ attribute, which indicates it's callable.
Example
# creating a function
def addition(p, q):
return p + q
# checking if objects have __call__ attribute
print("Does 'addition' have __call__?", hasattr(addition, '__call__'))
number = 10
print("Does 'number' have __call__?", hasattr(number, '__call__'))
# testing with a class instance
class MyClass:
def __call__(self):
return "I'm callable!"
obj = MyClass()
print("Does 'obj' have __call__?", hasattr(obj, '__call__'))
Does 'addition' have __call__? True Does 'number' have __call__? False Does 'obj' have __call__? True
Using the isinstance() Function
The isinstance() function checks if an object is an instance of a specific type. We use types.FunctionType to check for functions.
Example
import types
# creating a function
def addition(p, q):
return p + q
# checking if variables are instances of FunctionType
print("Is 'addition' a FunctionType?", isinstance(addition, types.FunctionType))
number = 10
print("Is 'number' a FunctionType?", isinstance(number, types.FunctionType))
# testing with lambda function
square = lambda x: x ** 2
print("Is 'square' a FunctionType?", isinstance(square, types.FunctionType))
Is 'addition' a FunctionType? True Is 'number' a FunctionType? False Is 'square' a FunctionType? True
Comparison of Methods
| Method | Detects User Functions | Detects Built-ins | Detects Callable Objects |
|---|---|---|---|
callable() |
Yes | Yes | Yes |
inspect.isfunction() |
Yes | No | No |
type() |
Yes | No | No |
hasattr() |
Yes | Yes | Yes |
isinstance() |
Yes | No | No |
Conclusion
Use callable() to check if an object can be called. Use inspect.isfunction() or isinstance() with types.FunctionType to specifically detect user-defined functions. Each method serves different purposes depending on your specific requirements.
