import time
def return_abc():
alphabet_list = []
for alphabet in "ABC":
time.sleep(1)
alphabet_list.append(alphabet)
return alphabet_list
print(return_abc())
## 출력값
['A', 'B', 'C']
for alphabet in return_abc():
print(alphabet)
## 출력값
...3초 후
A
B
C
import time
def yield_abc():
for alphabet in "ABC":
time.sleep(1)
yield alphabet
print(yield_abc())
## 출력값
<generator object yield_abc at 0x104fa5620>
for alphabet in yield_abc():
print(alphabet)
## 출력값
...1초 후
A
...1초 후
B
...1초 후
C
def normal_hello(time: str):
greeting = "good "
return greeting + time + ' Lighthouse'
def start_normal_greeting():
print(normal_hello("morning"))
print(normal_hello("afternoon"))
print(normal_hello("evening"))
start_normal_greeting()
## 출력값
good morning Lighthouse
good afternoon Lighthouse
good evening Lighthouse
def coroutine_hello():
greeting = "good "
while True:
text = (yield greeting)
greeting += text
def starting_corutine_greeting():
cr = coroutine_hello()
next(cr)
print(cr.send("morning"))
print(cr.send("afternoon"))
print(cr.send("evening"))
starting_corutine_greeting()
def coroutine_hello():
greeting = "good "
text = yield
while True:
text = yield greeting + text + ' Lighthouse'
def starting_corutine_greeting():
cr = coroutine_hello()
next(cr)
print(cr.send("morning"))
print(cr.send("afternoon"))
print(cr.send("evening"))
starting_corutine_greeting()
## 출력값
good morning Lighthouse
good afternoon Lighthouse
good evening Lighthouse
print(cr.send("morning"))
print(cr.send("afternoon"))
print(cr.send("evening"))