memory를 효율적으로 사용
- next() 메소드를 통해 차례로 값에 접근할 때마다 메모리에 적재하는 방식
- Lazy evaluation 즉 계산 결과 값이 필요할 때까지 계산을 늦추는 효과를
yield 있는 함수는 함수의 context포함한 generator 객체를 반환
generator object는 iterator 프로토콜을 제공해서 ierable함
제너레이터는 정해진 이터레이션이 끝나면 StopIteration Exception예외처리 해줘야
Python Generator Usages
Watch out for this (async) generator cleanup pitfall in Python
Watch out for this common generator pitfall.
What happens to a try/finally, with block, or async with block inside a generator if the generator isn't exhausted? Does the cleanup code still run? When and how does it run? In this video we take a look at the answers to these questions and learn how to avoid a common situation where cleanup code doesn't run when you want it to.
― mCoding with James Murphy (https://mcoding.io)
PEP 533: https://peps.python.org/pep-0533/
Discussion on GH issue: https://github.com/python/cpython/issues/88684
Source code: https://github.com/mCodingLLC/VideosSampleCode
SUPPORT ME ⭐
---------------------------------------------------
Sign up on Patreon to get your donor role and early access to videos!
https://patreon.com/mCoding
Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)
https://www.paypal.com/donate/?hosted_button_id=VJY5SLZ8BJHEE
Want to donate crypto? Check out the rest of my supported donations on my website!
https://mcoding.io/donate
Top patrons and donors: Laura M, Neel R, Dragos C, Jameson, Matt R, Pi, Vahnekie, Johan A, Mark M, Mutual Information
BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Discord: https://discord.gg/Ye9yJtZQuN
Github: https://github.com/mCodingLLC/
Reddit: https://www.reddit.com/r/mCoding/
Facebook: https://www.facebook.com/james.mcoding
CHAPTERS
---------------------------------------------------
0:00 Intro
1:21 The cleanup question
5:30 The async case
https://youtu.be/N56Jrqc7SBk?si=j0djQJehM2h0aPg_


Seonglae Cho