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)
Argument | object | Any Python object such as a custom object, a list, a function, a class, or any other object. |
Return Value | Boolean: True/False | Returns True if the object can be called with object()Returns False otherwise. |
Here are some basic usages of the function:
Input :callable(42)
Output :Input :
False
callable(int)
Output :Input :
True
callable(callable)
Output :True
Want to learn more? We’re going to dive into more examples next!
Check out my 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).
Publisher 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!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.