English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Python

Contrôle de flux Python

Fonction en Python

Types de données en Python

Opérations de fichier Python

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python callable() usage and examples

Python built-in functions

If the object passed is displayed as callable, the callable() method will return True. If not, it will return False.

The syntax of callable():

callable(object)

Callable() parameters

The callable() method takes a single parameter object.

Callable() return value

The callable() method returns:

  • True -If the object looks callable

  • False -If the object is not callable.

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

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

Example1: How callable() works?

x = 5
print(callable(x))
def testFunction():
  print("Test")
y = testFunction
print(callable(y))

When running the program, the output is:

False
True

Here, the objectxis not callable. And, the objectyIt seems to be callable (but may not be callable).

Example2: Callable object 

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

When running the program, the output is:

True

The example of Foo class seems to be callable (in this case, it can be called).

class Foo:
  def __call__(self):
    print('Print Something')
InstanceOfFoo = Foo()
# Prints 'Print Something'
InstanceOfFoo()

Example3: The object seems to be callable but not callable.

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

When running the program, the output is:

True

The example of Foo class seems to be callable, but it cannot be called. The following code will raise an error.

class Foo:
  def printLine(self):
    print('Print Something')
print(callable(Foo))
InstanceOfFoo = Foo()
# Raises an error
# The 'Foo' object is not callable
InstanceOfFoo()

Python built-in functions