-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_range.py
32 lines (27 loc) · 882 Bytes
/
my_range.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
#!/usr/bin/env python3
""" A play at generators
Simulate the behaviour of the built-in range expression
NOTE:
A generator is an object that generates a sequence of values.
Code that uses a generator may obtaian one value at a time without the
possibility of revisiting earlier values. (consume a generator's product)
"""
def range_custom(arg1, arg2=None, step=1):
"""(int, int, int) - generator
Simulate the behaviour of the built-in range expression
"""
if arg2 != None: # Do we have atleast two args
begin = arg1
end = arg2
else: # When it's one arg, default start at zero
begin = 0
end = arg1
i = begin
while i != end:
yield i
i += step
if __name__ == '__main__':
for num in range_custom(1, 11, 2):
print(num, end=' ')
for i in range(10):
pass