# inclusive_range
# start, stop, step
class Inclusive_range():
def __init__(self, *args):
num_args = len(args)
if num_args < 1:
raise TypeError('requires at least one argument')
elif num_args == 1:
self.start = 0
self.stop = args[0]
self.step = 1
elif num_args == 2:
(self.start, self.stop) = args
self.step = 1
elif num_args == 3:
(self.start, self.stop, self.step) = args
else:
raise TypeError('expected at most 3 arguments, got {}'.format(num_args))
def __iter__(self):
i = self.start
while i <= self.stop:
yield i
i += self.step
o = Inclusive_range(5, 10, 3)
for i in o:
print(i, end = ' ')
# 결과
5 8
댓글 영역