callable () in Python

Python Methods and Functions

In general, the callable — this is what can be called. This built-in method in Python checks and returns True if the passed object appears to be callable, but may not be, otherwise False. 
Syntax :

callable(object)

The callable () method takes only one argument, an object, and returns one of two values:

  • returns True if the object appears to be callable.
  • returns False if the object is not called.

Note: there may be several cases where callable () returns true, but the call to the object fails. But if case returns False, the caller will never succeed.

Case: when the object is called

# Python program for illustration
# callable ()
# test function

def Geek ():

return 5

  
# object created from Geek ()

let = Geek

print ( callable (let))

 
# test variable

num = 5 * 5

print ( callable ( num))

Output:

 True False 

Explanation :

  • Here we see in the first case, when an object is passed in the callable () method, it returns True. This is because let is the object of the called function Geek (which may not be the case in all cases).
  • In the second case, num is absolutely not callable, so the result is False.

The built-in callable () method checks if the argument is one of two things:

  • An instance of a class with the method __call__ .
  • Has a type that has a that indicates whether it can be called, for example, in functions, methods, etc., or has a nonzero member tp_call (c struct)

Example:

# Python program for illustration
# callable ()

class Geek:

def __ call __ ( self ):

print ( 'Hello GeeksforGeeks' )

 
# Assumes Geek can be called

print ( callable (Geek))

 
# This proves the class can be called

GeekObject = Geek ()

GeekObject ()

Output:

 True Hello Python.Engineering 

Explanation: Since the first case returns and prints True, this assumes the Geek class may can be called. After that we can call the __call__, method and it is available, which proves that the class can be called.

Case: when the object is NOT called

Let's see what happens in this example:

# Python program for illustration
# callable ()

class Geek:

def testFunc ( self ):

  print ( 'Hello GeeksforGeeks' )

 
# Assumes that the Geek class can be called

print ( callable (Geek))

 

GeekObject = Geek ()

# The object will be created, but
# returns an error when called
GeekObject ( )

Output:

 True 

Explanation: The callable () method returns True, assuming the Geek class is callable, but the Geek instance is not called () and 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 courtesy of Chinmoy Lenka . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you'd like to share more information on the topic discussed above.