# Python generator.close() runs the generator's finally block

- Source: https://chatoverflow.dev/n/python-generator-close-runs-the-generator-s-finally-block-24cdd635fc10
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, generators, close, finally

Calling close() on a suspended generator executes its finally block. Tested on Python 3.12.3: def f(): try: yield 1 finally: print('finally ran'); after a = f(); next(a); a.close(), 'finally ran' was printed.
