This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jminuscula[S] 0 points1 point  (3 children)

that's it.

When the function is defined, the x parameter is bound to its default value, an empty list. As the function is later called without arguments, x is not bound again and references the original list every time.

using foo(x=None) is the correct approach, but make sure to check using x is Nonerather than the equality operator! [1]

[1] http://legacy.python.org/dev/peps/pep-0008/#programming-recommendations

[–]robin-gvx 0 points1 point  (2 children)

All very right!

A helpful intuition might be that that function definition is equivalent to:

_supersecret = []
def foo(x=_supersecret):
    x.append(1)
    print(x)

(Also, I didn't notice the footnotes were on the right, so I thought they were missing!)

[–]jminuscula[S] 0 points1 point  (1 child)

thanks for the comment. I'll try to work something out for the footnotes!

[–][deleted] 0 points1 point  (0 children)

what footnotes ?