Python: Check if z Is a Function (Easy Guide)
In this article
To check if z is a function in Python, use callable(z). It returns True for functions, methods, classes, and any object with a __call__ method, and False otherwise. If you specifically need to confirm that z is a function and not another callable object, combine callable() with inspect.isfunction() or types.FunctionType. This guide shows both approaches and explains when each is appropriate.
What You'll Need
- Python 3 installed and working (any recent version).
- A code editor or an interactive Python shell.
- Basic familiarity with variables and functions.
Step-by-Step
- Start with the simplest check:
callable(). If you only need to know whether you can callzlike a function, writecallable(z). This returnsTruefor built-in functions, user-defined functions, lambdas, methods, classes, and instances that define__call__. It returnsFalsefor integers, strings, lists, and most other objects. - Test it in the REPL. Open a terminal and run
python(orpython3). Then try:
This confirms the behavior before you add it to a larger script.z = len print(callable(z)) # True z = 42 print(callable(z)) # False - Decide whether you need a strict function check.
callable()is broad. If you must ensurezis specifically a Python function (not a class or a callable object), use theinspectmodule:import inspect; inspect.isfunction(z). This returnsTrueonly for user-defined functions and lambdas, not for built-ins, methods, or classes. - Use
types.FunctionTypefor an alternative strict check. Importtypesand compare:isinstance(z, types.FunctionType). This behaves similarly toinspect.isfunctionand is useful if you prefer not to importinspect. - Handle built-in functions separately. Built-ins like
lenandprintare nottypes.FunctionType. To include them, checkinspect.isbuiltin(z)or simply rely oncallable(). - Write a reusable helper. If you check often, define:
Adjust the logic to match your exact definition of "function".import inspect def is_function(obj): return inspect.isfunction(obj) or inspect.isbuiltin(obj) - Test with different objects. Run your helper against a lambda, a class, a method, and a plain value to see the differences. This prevents surprises later.
- Integrate the check into your code. Use it in validation, for example:
if not callable(z): raise TypeError("z must be callable"). Choose the strict or broad check based on what your code will do withz.
Common Mistakes
- Assuming
callable()means "is a function". Classes and callable instances also returnTrue. If you need a true function, useinspect.isfunction. - Forgetting that built-in functions are not
FunctionType.lenis callable but failsinspect.isfunction. Includeinspect.isbuiltinif needed. - Using
type(z) == functionincorrectly. The correct type istypes.FunctionType, andisinstanceis safer than direct type comparison. - Checking before
zis defined. This raisesNameError. Ensurezexists or use atry/except. - Confusing methods with functions. Bound methods are callable but not
FunctionType. Useinspect.ismethodif you need to detect them.
Tips
- Prefer
callable()when you only care about invoking the object; it is Pythonic and fast. - Use
inspect.isfunctionwhen you need to distinguish user-defined functions from other callables. - Document your definition of "function" in code comments to avoid confusion for others.
- Combine checks with
inspectpredicates likeismethod,isbuiltin, andisclassfor precise type filtering. - In performance-sensitive code,
callable()is cheaper than importing and callinginspectfunctions.
Troubleshooting
NameError: name 'z' is not defined— definezbefore checking, or wrap the check in atry/except NameError.callable(z)returnsTruebut callingz()fails — the object may require arguments or raise inside its__call__. Check the signature withinspect.signature(z).inspect.isfunction(z)returnsFalsefor a built-in — that is expected; useinspect.isbuiltin(z)orcallable(z)instead.- Unexpected
Truefor a class — classes are callable. If you need to exclude them, addnot inspect.isclass(z)to your condition. - Import errors — ensure you have
import inspectorimport typesat the top of your file.
Frequently Asked Questions
What is the difference between callable() and inspect.isfunction()?
callable() returns True for anything that can be called, including classes and objects with __call__. inspect.isfunction() returns True only for user-defined functions and lambdas.
How do I check if z is a function in Python 2?
Use callable(z) (available in Python 2) or inspect.isfunction(z). Note that Python 2 is end-of-life; prefer Python 3.
Can I check if z is a lambda?
Lambdas are functions, so inspect.isfunction(z) returns True. There is no reliable built-in way to distinguish a lambda from a def function by type alone.
Why does callable(z) return True for a class?
Classes are callable because calling them creates instances. If you need to exclude classes, combine with not inspect.isclass(z).
Is there a performance difference between callable() and inspect.isfunction()?
Yes, callable() is a built-in and generally faster. Use inspect only when you need its stricter checks.
What if z is a method?
Bound methods are callable but not FunctionType. Use inspect.ismethod(z) to detect them specifically.