Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Python: Check if z Is a Function (Easy Guide)

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Start with the simplest check: callable(). If you only need to know whether you can call z like a function, write callable(z). This returns True for built-in functions, user-defined functions, lambdas, methods, classes, and instances that define __call__. It returns False for integers, strings, lists, and most other objects.
  2. Test it in the REPL. Open a terminal and run python (or python3). Then try:
    z = len
    print(callable(z))  # True
    z = 42
    print(callable(z))  # False
    This confirms the behavior before you add it to a larger script.
  3. Decide whether you need a strict function check. callable() is broad. If you must ensure z is specifically a Python function (not a class or a callable object), use the inspect module: import inspect; inspect.isfunction(z). This returns True only for user-defined functions and lambdas, not for built-ins, methods, or classes.
  4. Use types.FunctionType for an alternative strict check. Import types and compare: isinstance(z, types.FunctionType). This behaves similarly to inspect.isfunction and is useful if you prefer not to import inspect.
  5. Handle built-in functions separately. Built-ins like len and print are not types.FunctionType. To include them, check inspect.isbuiltin(z) or simply rely on callable().
  6. Write a reusable helper. If you check often, define:
    import inspect
    
    def is_function(obj):
        return inspect.isfunction(obj) or inspect.isbuiltin(obj)
    Adjust the logic to match your exact definition of "function".
  7. 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.
  8. 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 with z.

Common Mistakes

  • Assuming callable() means "is a function". Classes and callable instances also return True. If you need a true function, use inspect.isfunction.
  • Forgetting that built-in functions are not FunctionType. len is callable but fails inspect.isfunction. Include inspect.isbuiltin if needed.
  • Using type(z) == function incorrectly. The correct type is types.FunctionType, and isinstance is safer than direct type comparison.
  • Checking before z is defined. This raises NameError. Ensure z exists or use a try/except.
  • Confusing methods with functions. Bound methods are callable but not FunctionType. Use inspect.ismethod if you need to detect them.

Tips

  • Prefer callable() when you only care about invoking the object; it is Pythonic and fast.
  • Use inspect.isfunction when 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 inspect predicates like ismethod, isbuiltin, and isclass for precise type filtering.
  • In performance-sensitive code, callable() is cheaper than importing and calling inspect functions.

Troubleshooting

  • NameError: name 'z' is not defined — define z before checking, or wrap the check in a try/except NameError.
  • callable(z) returns True but calling z() fails — the object may require arguments or raise inside its __call__. Check the signature with inspect.signature(z).
  • inspect.isfunction(z) returns False for a built-in — that is expected; use inspect.isbuiltin(z) or callable(z) instead.
  • Unexpected True for a class — classes are callable. If you need to exclude them, add not inspect.isclass(z) to your condition.
  • Import errors — ensure you have import inspect or import types at 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.