- return vs yield
Generator can create data instantly without creating multiple data in advance.
def return_abc():
return list("ABC")
def yield_abc():
yield "A"
yield "B"
yield "C"
>>> print(return_abc())
['A', 'B', 'C']
>>> print(yield_abc())
<generator object yield_abc at 0x7f4ed03e6040>
- yield vs yield from
With yield from, it transformates list immediately.
def yield_abc():
for ch in ["A", "B", "C"]:
yield ch
def yield_abc():
yield from ["A", "B", "C"]
'Basic Python' 카테고리의 다른 글
sys.path, getcwd (0) | 2023.10.19 |
---|---|
assert (0) | 2022.12.05 |
lambda (0) | 2022.11.29 |
vars (0) | 2022.08.25 |
__new__, __init__ (0) | 2022.07.13 |