Never Use [ ] as a Default Argument in Python...

This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).

Using mutable objects like lists, dictionaries, or sets as default arguments in Python functions is a common beginner pitfall. The mutable object is initialized once at function definition time, not on each call, meaning all calls share the same object. This leads to unexpected state accumulation across calls. The correct fix is to use None as the default and initialize the mutable object inside the function body, and optionally use .copy() when passing an existing mutable object to avoid mutation side effects.

8m watch time
279 Impressions