반응형 WEB/Python-Django29 Python 코루틴 ## result = False## while True:## line = (yield result) # result 값을 외부로 보내고, send로 다음 문자열을 line 변수로 받음## result = word in line # line 안에 word가 있는지 검사####f = find('Python') # Python이라는 단어를 찾는 제너레이터 생성##next(f) # 제너레이터를 초기화하고 첫 yield까지 실행(result=false)## ##print(f.send('Hello, Python!'))##print(f.send('Hello, world!'))##print(f.send('Python Script'))## ##f.close() # 제너레이.. 2025. 5. 31. Python 이터레이터 class TimeIterator: SECONDS_IN_DAY = 24 * 60 * 60 # 하루 86400초 기준으로 계산 def __init__(self, start, stop): self.start = start % self.SECONDS_IN_DAY self.stop = stop % self.SECONDS_IN_DAY self.length = (self.stop - self.start) % self.SECONDS_IN_DAY # 이터레이터 : 메모리 절약, 큰 반복작업시 유용, 코드를 더 깔끔하고 간결하게 def __iter__(self): # 이터레이터 초기화 : 반복 시작시에만 호출 self.cur.. 2025. 5. 31. Python 메서드 class Time: def __init__(self, hour, minute, second): self.hour = hour self.minute = minute self.second = second @staticmethod def is_time_valid(time_string): hour,minute,second = map(int,time_string.split(':')) return hour 2025. 5. 31. Python 제너레이터 def countdown(n): a = n+1 def count(): nonlocal a a -= 1 return a return countn = int(input())c = countdown(n)for i in range(n): print(c(), end=' ')##IDLE에서 여러 줄 주석을 편하게 다는 법##메뉴에서 Format > Comment Out Region 클릭 (또는 Alt + 3 단축키)##주석 해제 : Alt + 4##다른 툴에서는 Ctrl+/##제너레이터란?##일반 함수처럼 생겼지만, 값을 return 하지 않고 yield를 사용해 하나씩 반환합니다.##값을 하나 생성하고 멈췄다가, 다음 호출 때 이어서 다시 실행됩니다.##.. 2025. 5. 31. 이전 1 ··· 3 4 5 6 7 8 다음 반응형