What is the correct fix?
def add_item(item, bag=[]):
bag.append(item)
return bag
- Change default to
bag=Noneand init inside - Change
appendtoextendin the body - Return
iteminstead of the bag list - Remove the default value from the signature
A mutable default is shared across calls; use None then init.
Play Problem Solving ▸