Question: Need help understanding and running this program 3. [10] Objects can behave like functions. Objects aren't functions. But they can be made callable, which allows
3. [10] Objects can behave like functions. Objects aren't functions. But they can be made callable, which allows you to treat them like functions. If an object is callable, you can use the function call syntax on it. This is all powered by the __call__ method. Here's an example of class defining a callable object: class Adder(object): def _init_(self, n): self.n=n def _call_(self, x): return self.n + x >>> plus = Adder(5) #plus behaves like a function which adds 5 to its argument >>> plus (4) 9 >>> plus(10) 15 Basically, "calling" an object as a function will execute the object's __call_method automatically. Find the output printed by the following program: class Special(object): def _init__(self, n): self.n def call_(self, x, y): return self.n * x + y En 1 obj Special(3) X = obj(2,4) print(x) print(obj(4,2)) # prints # prints
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
