Last active
August 21, 2021 19:23
-
-
Save carcigenicate/5a46506c1dfa2de33be652a64c30942a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BadIterable: | |
def __iter__(self): | |
class Iterator: | |
def __init__(self): | |
self.n = 0 | |
def __next__(self): | |
return self.n | |
def __iter__(self): | |
return self | |
def advance(self, by): | |
self.n += by | |
def previous(self, by): | |
self.n -= by | |
return Iterator() | |
it = iter(BadIterable()) | |
for n in it: | |
if n % 3 == 0: | |
it.previous(3) | |
print(n) | |
it.advance(2) | |
if n > 100: | |
break | |
# Prints | |
# 0 | |
# -1 | |
# 1 | |
# 3 | |
# 2 | |
# 4 | |
# 6 | |
# 5 | |
# 7 | |
# 9 | |
# 8 | |
# 10 | |
# 12 | |
# 11 | |
# 13 | |
# 15 | |
# 14 | |
# 16 | |
# 18 | |
# 17 | |
# 19 | |
# 21 | |
# 20 | |
# 22 | |
# 24 | |
# 23 | |
# 25 | |
# 27 | |
# 26 | |
# 28 | |
# 30 | |
# 29 | |
# 31 | |
# 33 | |
# 32 | |
# 34 | |
# 36 | |
# 35 | |
# 37 | |
# 39 | |
# 38 | |
# 40 | |
# 42 | |
# 41 | |
# 43 | |
# 45 | |
# 44 | |
# 46 | |
# 48 | |
# 47 | |
# 49 | |
# 51 | |
# 50 | |
# 52 | |
# 54 | |
# 53 | |
# 55 | |
# 57 | |
# 56 | |
# 58 | |
# 60 | |
# 59 | |
# 61 | |
# 63 | |
# 62 | |
# 64 | |
# 66 | |
# 65 | |
# 67 | |
# 69 | |
# 68 | |
# 70 | |
# 72 | |
# 71 | |
# 73 | |
# 75 | |
# 74 | |
# 76 | |
# 78 | |
# 77 | |
# 79 | |
# 81 | |
# 80 | |
# 82 | |
# 84 | |
# 83 | |
# 85 | |
# 87 | |
# 86 | |
# 88 | |
# 90 | |
# 89 | |
# 91 | |
# 93 | |
# 92 | |
# 94 | |
# 96 | |
# 95 | |
# 97 | |
# 99 | |
# 98 | |
# 100 | |
# 102 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment