Python callable() Function with Examples

callable() Function in Python:

If the specified object is callable, the callable() function returns true; otherwise, it returns False.

Syntax:

callable(object)

Parameters

object: It is an object that you want to see if an object is callable or not.

Return Value:

The callable() method yields:

  • True if the object appears to be callable.
  • False if the object cannot be called.

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

If callable() returns False, the call to the object will almost certainly fail.

Examples:

Example1:

Input:

Given Number = 10

Output:

Checking if the given number is callable or Not =  False

Explanation:

A normal variable cannot be called(not callable)

Example2:

Input:

Given Number = 25

Output:

Checking if the given number is callable or Not =  False

callable() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the callable() method that returns True if the given number is callable. Otherwise, it returns False.
  • Store it in another variable.
  • Print the result after checking if the given number is callable or Not.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 10
# Pass the given number as an argument to the callable() method that returns True if
# the given number is callable. Otherwise, it returns False.
# Store it in another variable.
rslt = callable(gvn_numb)
# Print the result after checking if the given number is callable or Not.
print("Checking if the given number is callable or Not = ", rslt)

Output:

Checking if the given number is callable or Not =  False
For Functions

Approach:

  • Create a function say num.
  • Inside the function, take a variable and initialize it with some random variable.
  • Inside the main function, pass the above variable function attribute to the callable() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Create a function say num.
def num():
    # Inside the function, take a variable and initialize it with some random variable.
    y = 10


# Inside the main function, pass the above variable function attribute to
# the callable() function and print it.
print(callable(num))

Output:

True

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the callable() method that returns True if the given number is callable. Otherwise, it returns False.
  • Store it in another variable.
  • Print the result after checking if the given number is callable or Not.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Pass the given number as an argument to the callable() method that returns True if
# the given number is callable. Otherwise, it returns False.
# Store it in another variable.
rslt = callable(gvn_numb)
# Print the result after checking if the given number is callable or Not.
print("Checking if the given number is callable or Not = ", rslt)

Output:

Enter some random number = 25
Checking if the given number is callable or Not = False