(question)
Question:
Answer:
Question:
#!/usr/bin/python
def add_evil(data=[]):
data.append(666)
return data
print add_evil()
print add_evil(["hi there"])
print add_evil()
print add_evil()
The output is:
Using mutable default values is not recommended.
Python has a bizarre way of handling default parameters. It binds the default value once, when the function is defined, instead of doing it each time the function is called. So, if a default value is mutable, its value may be different on successive calls.[666] ['hi there', 666] [666, 666] [666, 666, 666]
Using mutable default values is not recommended.