A software engineer and an Arch Linux enthusiast.
With years of experience working closely with open-source systems, he is passionate about building efficient, customizable solutions that put the user in control.
Abstract
Python 的 list comprehension 結合 lambda 表達式雖然寫法簡潔,卻容易造成變數綁定上的誤解。
以下面程式為例,其輸出結果並不像直覺上那麼簡單:
lst = [lambda: i for i in range(5)]
本次將依據 PEP 227 規範,深入解析 Python 變數作用域及記憶體管理的運作原理。並設計實驗驗證 CPython 的實際行為確實遵循規範且可被觀察重現。