The Mutable Default Trap

The Dangerous Way
def add_waypoint(pt, route=[]):
    route.append(pt)
    return route
Memory Allocation
🎒 Shared Default List
The Safe Way
def add_waypoint(pt, route=None):
    if route is None:
        route = []
    route.append(pt)
Memory Allocation
Click add_waypoint('Bear'). Python will allocate memory for both functions.