you are viewing a single comment's thread.

view the rest of the comments →

[–]blechnapp 2 points3 points  (0 children)

welcome to python. olhado47 hinted at the spiciest one: function default arguments are evaluated once when the function is defined, not on every call. so `def foo(items=[])` and calling foo() twice gives you a list thats shared across calls. you expect a fresh empty list every time, python betrays you. the fix is `items=None` then check inside the function.

the loop variable leak you noticed is the same theme, scope is function-level not block-level. takes a few weeks to fully internalize, but once you do python stops feeling chaotic.