
Python functions created inside loops often return the same value because closures capture a variable, not a snapshot of its value. This behavior is known as late binding. When the loop finishes, all functions reference the final value of the loop variable. This article explains why it happens and demonstrates two standard solutions: default argument capture and factory functions.
View original source — Hacker Noon ↗
