https://qiita.com/ganariya/items/fb3f38c2f4a35d1ee2e8
In order to study Python, I copied a swarm intelligence library called acopy.
In acopy, many interesting Python grammars and idioms are used, and it is summarized that it is convenient among them.
This time, let's look at the __call__
attribute that calls an instance of a class like a function.
__call__
attribute methodIf you define the __call__
attribute in a class,
You can call an instance like a function with the ()
operator.
class Person:
def __init__(self, age, name):
self.age = age
self.name = name
def __repr__(self):
return f'age = {self.age}, name = {self.name}'
def __call__(self, *args, **kwargs):
return self.name
'''
age = 20, name = ron
ron
'''
p = Person(20, 'ron')
print(p)
print(p())
In the above example, the __call__
attribute is defined in the Person class.
This will return the name of p when the instance is called, like a function call.
In this way, it is __call__
that allows you to call it like a function.
So how happy is it with this ()
?
I don't think it should be done much, but as a simple interface I wonder if it can be used.
class Add:
def __init__(self, x):
self.x = x
def __call__(self, y):
return self.x + y
class Sub:
def __init__(self, x):
self.x = x
def __call__(self, y):
return self.x - y
'''
20
10
50
'''
arr = [Add(10), Sub(20), Add(40)]
for ice in arr:
print(ice(10))
I think it's something different.
I think this is mainly the meaning of this.
In C ++, there is a function object that has ()
in the struct.
This is my article that touched on it. (There is no reason why my unordered_map can't use pair type as a key)
It's hard to give a function a state, but a class can easily have a state.
Then, it is possible to get the number of times the ()
operator is used and the calculation result such as DP of heavy processing.
class Count:
cls_count = 0
def __init__(self):
self.count = 0
def __call__(self, *args, **kwargs):
self.count += 1
Count.cls_count += 1
return self.count, Count.cls_count
c1 = Count()
c2 = Count()
'''
(1, 1)
(1, 2)
(2, 3)
(3, 4)
(2, 5)
'''
print(c1())
print(c2())
print(c1())
print(c1())
print(c2())
In the above code, we create a Count class and call it as a function.
Internally, the variables and internal states cls_count, count
are saved.
The above is a good example, but you can save the internal state in a function call called ()
.
Also, the following is a function call to the Eratosthenes sieve. After calculating once, the internally cached value is returned.
class Era:
def __init__(self, N):
self.N = N
self.primes = []
self.ready = False
def __call__(self, x):
if self.ready:
return self.primes[x]
else:
self.primes = [True] * (self.N + 1)
self.primes[0] = self.primes[1] = False
i = 2
while i * i <= self.N:
if self.primes[i]:
for j in range(i * 2, N + 1, i):
self.primes[j] = False
i += 1
return self.primes[x]
N = 100
era = Era(N)
for i in range(0, N + 1):
print(i, era(i))
In this way, I want to call it like a function with ()
from the outside, but when I want to have an internal state, it seems good to use __call__
.
-About the call method -A story I didn't understand call and callable
Recommended Posts