Python callable() Function

Python’s built-in callable(object) returns True if you could call the object argument like a function with the trailing parentheses in object(). You can make any object callable by implementing the instance’s __call__() method. For example, callable(callable) returns True because callable is a function object. But callable(3) returns False because an integer is not a function you can call.

Here’s a minimal example:

>>> callable(callable)
True
>>> callable(3)
False

Note: The function callable() was first removed in Python 3.0 but then reintroduced in version 3.2 and above.

Syntax: callable(object)
ArgumentobjectAny Python object such as a custom object, a list, a function, a class, or any other object.
Return ValueBoolean: True/FalseReturns True if the object can be called with object()
Returns False otherwise.

Here are some basic usages of the function:

Input : callable(42)
Output : False

Input : callable(int)
Output : True

Input : callable(callable)
Output : True

Want to learn more? We’re going to dive into more examples next!


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners

How to Check Whether a Function Object is Callable

The following code shows you how to use the callable() method to check whether an arbitrary object is a function, method, or another callable object:

def finxter():
    return 'Python'


print(callable(finxter))
# True

f = finxter

print(callable(f))
# True

print(callable(finxter()))
# False

How to Create Your Own Callable Object

The following code shows you how to create your own callable object.

class Car:
    def __call__(self):
        print('brumm')


porsche = Car()

# Is it callable?
print(callable(porsche))
# True

# Call it!
porsche()
# brumm

This is an interesting way to make any instance instantly callable so that it can be used as a function.

Summary

Python’s built-in callable(object) returns True if you can call the object argument like a function with the trailing parentheses in object().

Otherwise, it returns False.

For example, callable(callable) returns True because callable is a function object. But callable(3) returns False because an integer is not a function you can call.

You can make any object callable by implementing the instance’s __call__() method.


Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!