$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #28

What is the correct fix?

def add_item(item, bag=[]):
    bag.append(item)
    return bag
  1. Change default to bag=None and init inside
  2. Change append to extend in the body
  3. Return item instead of the bag list
  4. Remove the default value from the signature
A mutable default is shared across calls; use None then init.
Play Problem Solving ▸