Which fix removes the bug?
def add_tag(tag, tags=[]):
tags.append(tag)
return tags
print(add_tag("a"), add_tag("b"))
- Default to
Noneand build inside - Default to
listand build inside - Return
tags[:]at the end instead - Move
tags.append(tag)above the def
A mutable default is created once and shared by every call, so the list keeps growing.
Play Problem Solving ▸