summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics/getitem.py
blob: f39296aca30a2e116f85da67ac13ff7739b59410 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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