0

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
Community
  • 1
  • 1
Naveen
  • 230
  • 1
  • 3
  • 12

1 Answers1

8

The code args=i runs when the function is defined, not when the function is called.

When you defined the function the value of i was 5. This means that the default value of args will always be 5, even if you later change the value of i to 6.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452