Op 19-11-15 om 13:45 schreef Steven D'Aprano: > On Thu, 19 Nov 2015 10:41 pm, BartC wrote: > > >> I expect the version with the default argument to be >> exactly the same as the last lot of calls, namely for: >> >> fn() >> fn() >> fn() >> >> to be equivalent to: >> >> temp=[] >> fn(temp) >> temp=[] >> fn(temp) >> temp=[] >> fn(temp) > > Why on earth would you expect that? > > I don't mean that it isn't sometimes useful. Of course it is sometimes > useful, there's no doubt about that. But why would you expect the language > to default to the *slow*, *expensive*, *complicated* behaviour instead of > the *fast*, *cheap*, *simple* behaviour?
I would say because it is better understandable. The way default value behave now, is just a little less surprising as when the following segment of code would print: [1] a = [] a.append(1) b = [] print b Let us not forget that the tutorial talks about Default Argument *Values*. And also the language reference talks about precomputed *values*. What we really get is a precomputed object. > You already have one way to set the argument to a fresh list every time you > call the function: put the code you want executed inside the body of the > function. There's no need for a *second* way to get the same result. Which is beside the point. The point is understandability. > But if you want the default value to be evaluated exactly once, and once > only, there is no real alternative to early binding. You could use a global > variable, of course, but that is no solution -- that's a problem waiting to > happen. No more than the situation we have now. The situation we have now is a problem waiting to happen too. Which is confirmed again and again. You are stacking the deck by calling a possible alternative "a problem waiting to happen" while ignoring the problems that happen with the current situation. -- https://mail.python.org/mailman/listinfo/python-list