You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is because __reversed__ swaps in a reversed iterator, then stores the resulting generator it gets form calling self.__iter__(), and swaps the original iterator back before returning the generator. Crucially, because __iter__() is a generator, none of its code is executed before the first element is required. So by the time you use the generator, it's using the original iterator again.
There are a number of ways to solve this. The top answer on https://stackoverflow.com/q/5724009/1961666 suggests wrapping most of the code in __iter__ in a generator function, so that __iter__ is not itself a generator and so the first line would be run immediately when called.
The text was updated successfully, but these errors were encountered:
read the [known issues]
environment, where applicable:
Version info:
4.65.0 3.10.11 (main, Apr 5 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)] linux
When
reversed
is used on tqdm, it unexpectedly returns everything in the original, unreversed, orderThis is because
__reversed__
swaps in a reversed iterator, then stores the resulting generator it gets form callingself.__iter__()
, and swaps the original iterator back before returning the generator. Crucially, because__iter__()
is a generator, none of its code is executed before the first element is required. So by the time you use the generator, it's using the original iterator again.There are a number of ways to solve this. The top answer on https://stackoverflow.com/q/5724009/1961666 suggests wrapping most of the code in
__iter__
in a generator function, so that__iter__
is not itself a generator and so the first line would be run immediately when called.The text was updated successfully, but these errors were encountered: