summaryrefslogtreecommitdiffstats
path: root/iterator.py
diff options
context:
space:
mode:
Diffstat (limited to 'iterator.py')
-rw-r--r--iterator.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/iterator.py b/iterator.py
new file mode 100644
index 0000000..2b1e14e
--- /dev/null
+++ b/iterator.py
@@ -0,0 +1,26 @@
+# this class implements an iterator
+# python needs to be told when to stop iterating, this is done by raising StopIteration exception
+
+class ReverseString():
+ def __init__(self, data):
+ self.data = data
+ self.index = len(data)
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ if self.index == 0:
+ raise StopIteration
+ self.index -= 1
+ return self.data[self.index]
+
+
+def main():
+ foo = ReverseString('foobar')
+ for c in foo:
+ print(c)
+
+
+if __name__ == "__main__":
+ main()