$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #76

Which fix removes the bug?

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

print(add_tag("a"), add_tag("b"))
  1. Default to None and build inside
  2. Default to list and build inside
  3. Return tags[:] at the end instead
  4. 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 ▸