1

What output would you expect for this python snippet?

    def some_fn(arg=[]):
        arg.append('value')
        print(arg)

    for i in range(5):
        some_fn()

I would expect arg to be [] every time I call some_fn() without arguments. As a matter of fact the instance of the default value is being kept.

Has my expectation just been wrong or is this a strange behavior in Python? At least in C++ default values are being assigned every time I call the function.

frans
  • 8,868
  • 11
  • 58
  • 132
  • Ok, I found http://effbot.org/zone/default-values.htm but I still find this strange and misleading... – frans Mar 27 '15 at 10:52
  • Why is this misleading? Defaults are part of the *function object*. They are not global, but your function object may well be. – Martijn Pieters Mar 27 '15 at 10:54

0 Answers0