CodeWitness

Fix a shared default list

Call the function twice without passing tags. The second result unexpectedly contains the first tag.

def add_tag(tag, tags=[]):
    tags.append(tag)
    return tags

Default argument expressions are evaluated once when the function is defined, not once per call.

Why does add_tag("second") return both tags?