{"id":36762,"date":"2023-02-11T15:18:51","date_gmt":"2023-02-11T15:18:51","guid":{"rendered":"https:\/\/naiveskill.com\/?p=36762"},"modified":"2023-02-11T15:18:54","modified_gmt":"2023-02-11T15:18:54","slug":"__call__-python","status":"publish","type":"post","link":"https:\/\/naiveskill.com\/__call__-python\/","title":{"rendered":"__call__ python function with examples in 2023"},"content":{"rendered":"\n

In this blog, we will learn about __call__ python<\/strong> function. We will understand the __call__ function with various examples. so let’s get started:<\/p>\n\n\n\n

What is __call__ method in python<\/h2>\n\n\n\n

The __call__<\/strong> is a special method in python<\/a> 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.<\/p>\n\n\n\n

The returns value of the __call__ method is the result of the function call. The call<\/strong> method can be overridden in a subclass to customize the behavior of an object<\/p>\n\n\n\n

Syntax of __call__<\/strong> method in python<\/h3>\n\n\n\n

Below is the syntax for defining the call<\/strong> method in a Python class:<\/p>\n\n\n\n

class ClassName:\n    def __init__(self, [arg1, arg2, ...]):\n        # Initialize instance variables\n        # ...\n\n    def __call__(self, [arg1, arg2, ...]):\n        # Define callable behavior\n        # ...<\/pre>\n\n\n\n

The call<\/strong> method takes at least one argument. Users can also pass additional arguments to the call<\/strong> method if needed. The return value of the call<\/strong> method is the result of the function call.<\/p>\n\n\n\n

__call__ python example<\/h2>\n\n\n\n

In this session, we will understand __call__ python function with several examples:<\/p>\n\n\n\n

__call__ python example 1<\/h3>\n\n\n\n

Let’s define a class Multiply which will multiply the two numbers<\/p>\n\n\n\n

class Multiply:\n    def __init__(self, value):\n        self.value = value\n        \n    def __call__(self, x):\n        return x * self.value\n\nmul1 = Multiply(5)\nresult = mul1(10)\nprint(result)<\/pre>\n\n\n\n

The Multiply class has a __call__ method which takes a single argument x and returns the result of x * self.value.<\/p>\n\n\n\n

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.<\/p>\n\n\n\n

\"__call__<\/figure>\n\n\n\n

__call__ python example 2<\/h3>\n\n\n\n

Below is another example of using the call<\/strong> method in Python:<\/p>\n\n\n\n

class Hello:\n    def __init__(self, text):\n        self.text = text\n\n    def __call__(self, name):\n        return self.text + \" \" + name\n\nhello = Hello(\"hello\")\nresult = hello(\"Jack\")\nprint(result) <\/pre>\n\n\n\n

Above code defines a Hello class that has a call<\/strong> method that takes a single argument name and returns the result of concatenating self.text and name with a space in between.<\/p>\n\n\n\n

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”.<\/p>\n\n\n\n

\"__call__<\/figure>\n\n\n\n

When to use __call__ python function<\/h2>\n\n\n\n

The call<\/strong> method in Python can be used in the below scenarios:<\/p>\n\n\n\n