__call__-python

__call__ python function with examples in 2023

In this blog, we will learn about __call__ python function. We will understand the __call__ function with various examples. so let’s get started:

What is __call__ method in python

The __call__ is a special method in python that allows an object to be called like a function. When an object has a call method, it can be treated as a callable object and can be invoked as if it were a function.

The returns value of the __call__ method is the result of the function call. The call method can be overridden in a subclass to customize the behavior of an object

Syntax of __call__ method in python

Below is the syntax for defining the call method in a Python class:

class ClassName:
    def __init__(self, [arg1, arg2, ...]):
        # Initialize instance variables
        # ...

    def __call__(self, [arg1, arg2, ...]):
        # Define callable behavior
        # ...

The call method takes at least one argument. Users can also pass additional arguments to the call method if needed. The return value of the call method is the result of the function call.

__call__ python example

In this session, we will understand __call__ python function with several examples:

__call__ python example 1

Let’s define a class Multiply which will multiply the two numbers

class Multiply:
    def __init__(self, value):
        self.value = value
        
    def __call__(self, x):
        return x * self.value

mul1 = Multiply(5)
result = mul1(10)
print(result)

The Multiply class has a __call__ method which takes a single argument x and returns the result of x * self.value.

An instance of the Multiply class is created and stored in the mul1 variable with the value set to 5. The mul1 variable can then be called like a function, passing 10 as the x argument, which returns the result of 50.

__call__ python

__call__ python example 2

Below is another example of using the call method in Python:

class Hello:
    def __init__(self, text):
        self.text = text

    def __call__(self, name):
        return self.text + " " + name

hello = Hello("hello")
result = hello("Jack")
print(result) 

Above code defines a Hello class that has a call method that takes a single argument name and returns the result of concatenating self.text and name with a space in between.

An instance of the Hello class is created and stored in the hello variable with text set to “hello”. The hello variable can then be called like a function, passing “Jack” as the name argument, which returns the result of “hello Jack”.

__call__ python example

When to use __call__ python function

The call method in Python can be used in the below scenarios:

  • The call method allows you to treat instances of a class as if they were functions, enabling you to call them like you would a regular function.
  • You can use the call function to implement a function-like interface to customize the behavior of instances of that class
  • The call method can be used to implement higher-order functions
  • You can use the call method to create objects that can be used as decorators.

More to Explore?

Python open function

Python xor function

Python str function

Python __init__ method

Python __all__ method

python __new__ method

Leave a Comment

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

Scroll to Top