Python callable() Method With Example

In this article, we will learn about python callable() method with example. We will see how to use it, it’s parameter, return value etc. with example.

Getting Started

A callable is something that can be called.

callable() method in python return True if object, passed as parameter, appears to be callable (However, it may not be callable). Otherwise, it returns False.

Syntax of callable() Method

Syntax of callable() method is –

 
callable(argument)

Where,
argument is any object.

Parameter of callable() Method

As shown above, callable() method takes only one argument – object.

Return Value of callable() Method

Python callable() method returns –

  • True: If object passed as parameter seems to be callable. However, it may not be callable.
  • False: If object is not callable.

Examples of callable() Method

There are 3 scenarios –

  1. Method returns True and object is callable
  2. Method returns True but object is not callable
  3. Method returns False and object is not callable
  1. Method returns True and object is callable

    • All built-in functions are callable. For example, bytes, bytearray, bool etc.
       
      print(callable(bytes))
      print(callable(bytearray))
      print(callable(bool))
      

      Output:

       
      True
      True
      True
      
    • All user-defined function created using def or lambda is callable. For example,
      def square(b):
          return b * b
      
      print(callable(square))
      print(callable(lambda y: y))
      

      Output:

      True
      True
      
    • All Methods are also callable. In fact, methods are functions attached with object of the class. Hence, it is callable. For example,

       
      class Person:
          def __init__(self):
              print("Init Called")
      
          def sayHello(self):
              print("Hello")
      
      print(callable(Person.sayHello))
      

      Output:

       
      True
      
    • If any class implements __call__ method, then instance of that class is callable. For example,
      class Person:
      
          def __call__(self):
              self.sayHello()
          
          def __init__(self):
              print("Init Called")
      
          def sayHello(self):
              print("Hello")
      
      
      person = Person()
      print(callable(person))
      

      Output:

      True
      
  2. Method returns True but object is not callable

    If a class does not implement __call__ method, then instance of that class is not callable. For example,

     
    class Person:
    
        def __init__(self):
            print("Init Called")
    
        def sayHello(self):
            print("Hello")
    
    print(callable(Person))
    
    person = Person()
    person()
    

    Output:

     
    True
    Init Called
    Traceback (most recent call last):
      File "main.py", line 12, in <module>
        person()
    TypeError: 'Person' object is not callable
    
  3. Method returns False and object is not callable

    Let’s take an example,

     
    square = 15 * 15
    print(callable(square))
    

    Output:

     
    False
    

    Certainly, variable square is not callable. Hence, output is False.

That’s how we can use python callable() method with example and check if passed argument is callable or not.
Learn more at – official doc.

We have covered python in detail at – https://tutorialwing.com/python-tutorial/

Leave a Reply