- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python callable() Function
The callable() function in Python returns True if the specified object appears to be callable, otherwise returns False if the specified object is not callable. For example:
def myf(): num = 10 print(callable(myf)) val = 20 print(callable(val))
The output produced by this Python program, demonstrating the callable() function is:
True False
Note: If callable() returns True for specified object. Then call to that object may fails. But if callable() returns False for specified object. Then call to that object will surely fail.
Python callable() Function Syntax
The syntax of callable() function in Python is:
callable(obj)
where obj refers to an object that is going to check whether it is a callable object or not.
Python callable() Function Example
Here is a simple example of callable() function in Python. In this program, I've created a class fresherearth to check whether it is callable or not using of course, the callable() function:
class fresherearth: def __call__(self): print("python programming") x = callable(fresherearth) if x: print("The class \"fresherearth\" is callable") # following two statements proves, fresherearth class is callable obj = fresherearth() obj() else: print("The class \"fresherearth\" is not callable")
The snapshot given below shows the sample output produced by this Python program:
Now let's create another program without __call__() to demonstrate that sometime the callable() returns True for a specified object that appears to be callable, but actually we can not call that object:
class fresherearth: def myf(self): print("python programming") x = callable(fresherearth) if x: print("The class \"fresherearth\" is callable") # Following two statements raises error # As this time the class's instance is actually not callable obj = fresherearth() obj() else: print("The class \"fresherearth\" is not callable")
If you execute this program, then the output would be:
Because using the statement:
x = callable(fresherearth)
The method callable() here returns True. Therefore True gets initialized to x. And because x is equal to True, therefore the condition if x or if True evaluates to be True. And the program flow goes inside the if block. And the statement:
print("The class \"fresherearth\" is callable")
gets executed. So that we're seeing:
The class "fresherearth" is callable
on the output before the error message. But while using the following two statements:
obj = fresherearth() obj()
actually using the last statement, an exception named TypeError is raised. Therefore don't believe on the callable() function only. That is, if it returns True, then do not assume that the object is actually callable. But still, you can catch the exception using the except block by putting the above two statements (or the last statement, the statement used to call) inside the try block as shown in the program given below:
class fresherearth: def myf(self): print("python programming") x = callable(fresherearth) if x: print("The class \"fresherearth\" appears to be callable") obj = fresherearth() try: obj() except TypeError: print("But actually is not callable") else: print("The class \"fresherearth\" is not callable")
Now the output produced by this program is shown in the snapshot given below:
That is, if the program does not raises an exception after executing the statement inside the try block, means that the object is surely callable. Therefore you can put all the things when the object is callable inside the try block.
« Previous Function Next Function »