How to use the __call__ method in a Python class

When creating a class, I use init a lot, but when I get call, I interpret it in my own way.

The environment is Python 3.5.1 :: Anaconda 2.5.0 (x86_64) Run on Jupyter notebook

__call__ is activated when called like a function

Class example

class_sample.py


class A:
    
    def __init__(self, a):
        self.a = a
        print("A init")
        
    def __call__(self, b):
        print("A call")
        print(b + self.a)
        
class B(A):
    
    def __init__(self, a, c):
        super().__init__(a)
        self.c = c
        print("B init")
    
    def __call__(self, d):
        print("B call")
        print(self.a + self.c + d)

Execution example

>>> a = A(1)
A init

>>> a(2)
A call
3

>>> b = B(1,3)
A init
B init

>>> b(4)
B call
8

Only __init__ is called for instantiation. However, if you call the created instance with arguments like a function, __call__ will be called. Of course, if you give a return value to __call__, you can use the value obtained from the instance for another variable.

Recommended Posts

How to use the __call__ method in a Python class
[Introduction to Python] How to use class in Python?
How to use __slots__ in Python class
How to use the C library in Python
[Introduction to Python] How to use the in operator in a for statement?
How to use the model learned in Lobe in Python
How to write a Python class
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
I tried "How to get a method decorated in Python"
How to get the last (last) value in a list in Python
[Python] How to make a class iterable
How to get a stacktrace in python
How to use regular expressions in Python
How to use is and == in Python
How to use python multiprocessing (continued 3) apply_async in class with Pool as a member
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
How to use the asterisk (*) in Python. Maybe this is all? ..
How to check the memory size of a dictionary in Python
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to use Python Image Library in python3 series
How to create a JSON file in Python
To dynamically replace the next method in python
Summary of how to use MNIST in Python
[Algorithm x Python] How to use the list
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
[Python] How to draw a histogram in Matplotlib
[Python] Explains how to use the range function with a concrete example
How to sort by specifying a column in the Python Numpy array.
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
How to call a function
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to develop in Python
[Python] How to use input ()
How to use the decorator
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to retrieve the nth largest value in Python
How to get the variable name itself in python
How to get the number of digits in Python
How to know the current directory in Python in Blender
How to use the Raspberry Pi relay module Python
[Python] How to expand variables in a character string
A memorandum on how to use keras.preprocessing.image in Keras
How to use bootstrap in Django generic class view
[Python] How to use the graph creation library Altair
How to use the exists clause in Django's queryset