티스토리 뷰

Python

클래스로 inclusive_range 구현

techbard 2016.05.12 14:46

# 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 


댓글
댓글쓰기 폼
공지사항
Total
407,537
Today
11
Yesterday
19
«   2019/09   »
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          
글 보관함