In general, a callable is something that can be called. This built-in method in Python checks and returns True if the object passed appears to be callable, but may not be, otherwise False.
The callable() method takes only one argument, an object and returns one of the two values:
- returns True, if the object appears to be callable.
- returns False, if the object is not callable.
Note: There may be few cases where callable() returns true, but the call to object fails. But if a case returns False, calling object will never succeed.
Case : When Object is callable
- Here, we see in the first case when an object is passed in the callable() method, it returns True. It is so because let is an object to the callable function Geek (which may not be in all cases).
- In the second case num is absolutely not a callable object, so the result is False.
The built-in callable() method checks if the argument is either of the two:
- An instance of a class with a __call__ method.
- Is of a type that has a which indicates callability such as in functions, methods etc. or has a non null tp_call (c struct) member
True Hello GeeksforGeeks
Explanation: Since the first case returns and prints True, it suggests that the class Geek may be callable. Following this, we are able to call the __call__ method and it is accessible, thus proving the class is callable.
Case : When Object is NOT callable
Let’s see what happens in this example:
Explanation: The callable() method returns True suggesting that the Geek class is callable, but the instance of Geek is not callable() and it returns a runtime error:
Traceback (most recent call last): File "/home/3979dc83032f2d29befe45b6ee6001a4.py", line 10, in GeekObject() TypeError: 'Geek' object is not callable
This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Turning a Function Pointer to Callable
- Python | Convert list to Python array
- Reading Python File-Like Objects from C | Python
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- gcd() in Python
- Python Set | pop()
- zip() in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.