Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
In the following code should the output not be 6 6 7 6 but the actual output is very different as mentioned below
i=5
def fs(args=i):
print args
print i
i=6
fs()
fs(7)
Actual Output is 5 6 7 6
The code
args=iruns when the function is defined, not when the function is called.When you defined the function the value of
iwas 5. This means that the default value ofargswill always be 5, even if you later change the value ofito 6.