May-13-2023, 02:43 PM
(This post was last modified: May-13-2023, 02:43 PM by Phidias618.)
Hello,
Yesterday I ran into an issue, i try to iterate multiple time trough a custom class,
the class is a chess Board and iterating trough it would give a tuple of (x, y, tile) for each step of the loop
to iterate multiple time I store all the variable that I will need in the __next__ method into a list and add that list to another list, iter_list
but to work the __next__ method of the class delete the last element of iter_list juste before raising StopIteration
that mean that if someone use break while iterating this class, the last element of iter_list would not be deleted
So that why I ask this question, I search online and i saw that a can use the context manager but that didn't look good for me
and I think that it isn't an obvious way to do it
I think that a __break__ method would be usefull to iterate multiple time into a custom class and still use break inside the loop:
like this:
for *var0 in custom_iterator:
----for *var1 in custom_iterator:
--------# do something
--------if some_condition:
------------break
So I wonder why there is not __break__ method
Yesterday I ran into an issue, i try to iterate multiple time trough a custom class,
the class is a chess Board and iterating trough it would give a tuple of (x, y, tile) for each step of the loop
to iterate multiple time I store all the variable that I will need in the __next__ method into a list and add that list to another list, iter_list
but to work the __next__ method of the class delete the last element of iter_list juste before raising StopIteration
that mean that if someone use break while iterating this class, the last element of iter_list would not be deleted
So that why I ask this question, I search online and i saw that a can use the context manager but that didn't look good for me
and I think that it isn't an obvious way to do it
I think that a __break__ method would be usefull to iterate multiple time into a custom class and still use break inside the loop:
like this:
for *var0 in custom_iterator:
----for *var1 in custom_iterator:
--------# do something
--------if some_condition:
------------break
So I wonder why there is not __break__ method