Python callable()

The syntax of callable() is:

callable(object)

callable() Parameters

callable() method takes a single argument object.


Return value from callable()

callable() method returns:

  • True - if the object appears callable
  • False - if the object is not callable.

It important to remember that, even if callable() is True, call to the object may still fail.

However, if callable() returns False, call to the object will certainly fail.


Example 1: How callable() works?

x = 5
print(callable(x))

def testFunction():
  print("Test")

y = testFunction
print(callable(y))

Output

False
True

Here, the object x is not callable. And, the object y appears to be callable (but may not be callable).


Example 2: Callable Object

class Foo:
  def __call__(self):
    print('Print Something')

print(callable(Foo))

Output

True

The instance of Foo class appears to be callable (and is callable in this case).

class Foo:
  def __call__(self):
    print('Print Something')

InstanceOfFoo = Foo()

# Prints 'Print Something'
InstanceOfFoo()

Example 3: Object Appears to be Callable but isn't callable.

class Foo:
  def printLine(self):
    print('Print Something')

print(callable(Foo))

Output

True

The Foo class appears to be callable, but an instance of it is not callable. The following code will raise an error.

class Foo:
  def printLine(self):
    print('Print Something')

print(callable(Foo))  # still returns True because Foo is a class (classes are callable)

InstanceOfFoo = Foo()
# Raises an Error
# 'Foo' object is not callable
InstanceOfFoo()  # error occurs because instances of Foo are not callable

Output

True
Traceback (most recent call last):
File "", line 10, in 
TypeError: 'Foo' object is not callable

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges