Do not use []
as a default value!
>>> def bad(default=[]):
... default.append(1)
... print default
...
>>> bad()
[1]
>>> bad()
[1, 1]
>>> bad()
[1, 1, 1]
>>> def good(default=None):
... if default is None:
... default = []
... default.append(1)
... print default
...
>>> good()
[1]
>>> good()
[1]
Default arguments are created when the function is defined, not when it is called.
So only use non-mutable types for defaults. using integers, strings, tuples is okay, but if you want a default list or dictionary or anything mutable then use None
and do the above trick.
Read this question and answers for better understanding of this matter.