How To Use Python Callable() Function Examples

The callable() function is used in Python to determine if an object is callable. In other words, it checks if the object can be called like a function. In this article, I will tell you what the python callable() function syntax is, how to use it with some examples.

1. Python callable() Function Definition.

  1. callable() is a built-in Python function that returns True if the specified object is callable (i.e., can be called like a function) and False otherwise.
  2. In other words, callable() checks whether an object is a function or a variable.
  3. The syntax for callable() function is as follows:
    callable(object)
  4. Here, object is the object that we want to check if it is callable or not.
  5. If the specified object is callable, callable() function returns True. Otherwise, it returns False.

2. Python callable() Function Examples.

2.1 Check whether a function or a string is callable.

  1. Here is an example of how to use python callable() function:
    def my_function():
        print("Hello, World!")    
    print(callable(my_function)) # Output: True
    
    my_string = "Hello, World!"
    print(callable(my_string)) # Output: False
  2. When you run the above code, you will get the below output in console.
    True
    False
  3. In the above example, my_function() is defined as a function and my_string is defined as a string.
  4. When we pass my_function to the callable() function, it returns True because my_function is a callable function.
  5. However, when we pass my_string to the callable() function, it returns False because my_string is not a callable function.

2.2 Check if a built-in function is callable.

  1. Below is the example code.
    print(callable(print))  # Output: True
  2. In this example, we are checking if the built-in print function is callable or not.
  3. Since print is a built-in function, it is callable and it will print True on the console.

2.3 Check if a class is callable.

  1. The below source code will demo how to check if a python class is callable or not.
    class MyClass:
        def __init__(self):
            self.name = "John"
            
        def say_hello(self):
            print("Hello, " + self.name)
    obj = MyClass()
    
    print("MyClass is callable : " + str(callable(MyClass)))  
    
    print("obj is callable : " + str(callable(obj)))  
    
    print("obj.say_hello is callable : " + str(callable(obj.say_hello)))  
    
  2. When you run the above example code, it will print out the below text on the screen.
    MyClass is callable : True
    obj is callable : False
    obj.say_hello is callable : True
  3. In this example, we are defining a simple class MyClass with a constructor and a method say_hello().
  4. We then create an object of the class and check if the class and the object are callable using the callable() function.
  5. Since MyClass is a class definition, it is callable.
  6. However, the object obj is not callable.
  7. Finally, the method say_hello() is callable because it is a function defined within the class.

2.4 Check if a lambda function is callable.

  1. Below is the example source code.
    my_lambda = lambda x: x**2
    print(callable(my_lambda))  # Output: True
  2. In this example, we are defining a lambda function my_lambda that takes a single argument x and returns its square.
  3. We then check if the lambda function is callable using the callable() function.
  4. Since the lambda function is a function in Python, it is callable.

2.5 Check if a function defined in a module is callable.

  1. Below is the example source code.
    import math
    print(callable(math.sqrt))  # Output: True
  2. In this example, we are importing the math module and checking if the sqrt() function defined in it is callable or not. Since sqrt() is a function, it is callable.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.