Earlier quoted context omitted.
This is most probably because you are trying to code in Python like you code in JS: this always leads to frustration. I started to have fun coding in JS the day I accepted it was not Python and that I had to structure and style my code differently. E.G: in Python you will use iteration a lot. A lot of a lot. But not so many callbacks. The reverse is true in JS.
Iterators and callbacks aren’t comparable. You don’t use them for the same thing.
But even without that, I'm not saying they are comparable. I'm saying the same API will use explicit anonymous callbacks in JS and something else in Python (decorators, subclassing, protocols, generators...). I'm saying that the same API will use __iter__ in Python and something else in JS (type conversion, proxy object, explicit method call...).
E.G, this is a Python pattern you'll find in contextlib or in pytest fixtures:
@somekindofregistration
def foo():
print('code that runs before')
try:
yield
except Stuff:
print('Error handling')
print('code that runs after')
This uses Python iteration mechanism to run code at 3 different times in a life cycle.While in JS, you would pass 3 callbacks.