Python's magic method __call__
is called whenever you attempt to call an object. Cls()()
is thus equal to Cls.__call__(Cls())
.
Functions are first class objects in Python, meaning they're just callable objects (using __call__
). However, __call__
itself is a function, thus it too has __call__
, which again has its own __call__
, which again has its own __call__
.
So Cls.__call__(Cls())
is thus equal to Cls.__call__.__call__(Cls())
and again equilevant to Cls.__call__.__call__.__call__(Cls())
and so on and so forth.
How does this infinite loop end? How does __call__
actually execute the code?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…