Signup/Sign In

Python callable() Function - Python Library Functions

Posted in Programming   LAST UPDATED: OCTOBER 14, 2021

    A callable is something that can be called. Here in python, there is a built-in function called callable() that returns TRUE if the object passed is callable else it returns False. In this article, we will learn what makes a python object callable type, how can we define __call__ method in a class to make instance of that class callable and what is its use along with few examples.

    Python Callable Function

    Let's see the syntax of callable() function,

    callable(object)

    This python callable function takes only one parameter and the parameter must be an object. It returns,

    1. True: If the object passed is callable.

    2. False: If the object passed is not callable.

    Ok, the callable() function will tell us whether an object is callable or not, now the question arises, what does being a callable means?


    What is a Callable?

    In python, a callable is anything that can be called, using parentheses and maybe with some arguments too. A function is a callable, python class is callable and we can even make instances of a class as callables.

    In every programming language, functions/methods are callables, but a class being callable is a bit unique for python. In python for creating an instance of a class XYZ, all we have to do is, kind of call the class XYZ() which makes it difficult or we can say confusing at times to distinguish between a function and a class in python.

    To make instance of any class callable, we can define the __call__ special method in that class. Using this technique we can attach behavior to class instances by defining the __call__ method which can even take arguments, and making the instance callable.

    The syntax of the __call__ method is:

    __call__(self, agrs*, kwargs*)

    For class instance, if the callable function returns true, it is not necessary that the class has defined the __call__ method, there can be other reasons for it as well, which we will see in the code examples below. But, if for a class instance the callable function returns false, then you can be sure that there is no __call__ method in that class.


    Example 1: Function is callable

    # a simple function
    def Studytonight():
        return 'I Love StudyTonight'
    
    # assigning the function to a variable object making the variable a callable
    ex1 = Studytonight
    print(callable(ex1))
    
    # ex2 is not a callable object
    ex2 = 10 % 2
    print(callable(ex2))

    Output:

    True
    False

    Explanation:

    1. In the first case, ex1 variable take sreference for the callable function Studytonight, which make ex1 a callable object so the function returns True.

    2. In the second case, ex2 variable is definitely not a callable object as it is just storing result of an expression, so the function returns False


    Example 2: Class and its Instance without __call__ method and with it

    As we mentioned earlier, a class is by default a callable, hence, in the code below the callable function will return true for the class, but not for the instance of the class.

    
    class Studytonight:
        def __init__(self):
            print("I love Studytonight")
    
    # check for class
    print(callable(Studytonight))
    # creating class instance
    st1 = Studytonight()
    # check for the class instance
    print(callable(st1))
    
    

    Output:

    True
    I love Studytonight
    False

    Now let's define the __call__ method in our class and then check whether the instance of the class then becomes a callable and what happens when we call it,

    # class with __call__ method
    class Studytonight:
        def __init__(self):
            print("I love Studytonight")
        # call method
        def __call__(self):
            print("This is my love calling Studytonight")
    
    # check for class
    print(callable(Studytonight))
    # creating class instance
    st1 = Studytonight()
    # check for the class instance
    print(callable(st1))
    # calling the class instance
    st1()

    Output:

    True
    I love Studytonight
    True
    This is my love calling Studytonight

    In the example above we have defined the __call__ method without any arguments, but we can define it with arguments too. As we have mentioned in the syntax of the __call__ method, it can take multiple arguments represented by args* and can also take multiple named arguments too represented by kwargs*.


    Conclusion:

    Pheww! This is it. I hope that you enjoyed the post and learned about the callable() python library function and doing so also got introduced to the __call__ function. If you feel that this post is useful, please share it with your friends and colleagues.

    Thanks for reading it till the end.

    You may also like:

    About the author:
    I am a self-taught Blogger and Programmer. I have good sort of knowledge in Technology, PHP, Javascript, HTML, CSS and Python.
    Tags:PythonProgrammingCallable
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS