-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake_class_iterable.py
47 lines (36 loc) · 1.03 KB
/
make_class_iterable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
# Date: 2018-09-02
#
# Description:
# Example to make an user defined class iterable.
class Test(object):
# Constructor
def __init__(self, limit):
print ('Calling __init__()...')
self.limit = limit
# Called when iteration is initialized
def __iter__(self):
"""
This is called when object of this class ready for iteration, when for
statement is used.
"""
print ('Calling __iter__()...')
self.x = 10
return self
# To move to next element. In Python 3,
# we should replace next with __next__
def next(self):
print ('Calling next()...')
# Store current value ofx
x = self.x
# Stop iteration if limit is reached
if x > self.limit:
raise StopIteration
# Else increment and return old value
self.x = x + 1;
return x
# Prints numbers from 10 to 15
test = Test(15)
print 'Object created...'
for i in test: # Here __iter__() will be called.
print(i)