summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics/getitem.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-25 00:17:36 +0000
committerDamien George <damien.p.george@gmail.com>2014-01-25 00:17:36 +0000
commit7c9c667633d445e4df88868d630a0af4bc63d2f8 (patch)
treeb130d6dc98ba1ecd47d8886ea13048a88a5a6865 /tests/basics/getitem.py
parentfcd4ae827171717ea501bf833a6b6abd70edc5a3 (diff)
downloadmicropython-7c9c667633d445e4df88868d630a0af4bc63d2f8.tar.gz
micropython-7c9c667633d445e4df88868d630a0af4bc63d2f8.zip
py: Implement iterator support for object that has __getitem__.
Addresses Issue #203.
Diffstat (limited to 'tests/basics/getitem.py')
-rw-r--r--tests/basics/getitem.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/basics/getitem.py b/tests/basics/getitem.py
new file mode 100644
index 0000000000..f39296aca3
--- /dev/null
+++ b/tests/basics/getitem.py
@@ -0,0 +1,22 @@
+# create a class that has a __getitem__ method
+class A:
+ def __getitem__(self, index):
+ print('getitem', index)
+ if index > 10:
+ raise StopIteration
+
+# test __getitem__
+A()[0]
+A()[1]
+
+# iterate using a for loop
+for i in A():
+ pass
+
+# iterate manually
+it = iter(A())
+try:
+ while True:
+ next(it)
+except StopIteration:
+ pass