I have read many online tutorials before , However, I just learned the theory , Rarely used in practice , So I don't know why , But these two functions are often used in recent work , So here is a comparison
call Is to make the defined classes look like ordinary functions , Direct use Class name ( Parameters )
You can call
getitem Let the defined class call iteratively
The following is directly represented by code
class RandomFlip():
def __init__(self, flip_prob=0.5):
self.flip_prob = flip_prob
def __call__(self, sample):
print('sample is :', sample)
def __getitem__(self, idx):
print('current idx is: ', idx)
How to use it is as follows
rf = RandomFlip()
rf[1] # Would call __getitem__
rf("20") # Would call __call__
>>>
current idx is: 1
sample is : 20