Good Code
The good version creates a new list only when the caller did not pass one.
Lesson 03
Avoid shared mutable defaults by creating fresh lists, dicts, or sets inside the function.
def add_review_tag(tag, tags=None):
if tags is None:
tags = []
tags.append(tag)
return tagsdef add_review_tag(tag, tags=[]):
tags.append(tag)
return tagsThe good version creates a new list only when the caller did not pass one.
The bad version shares the same default list across calls. Tags from one request or test can appear in the next call.