Antoon Pardon wrote: > I know what happens, I would like to know, why they made this choice. > One could argue that the expression for the default argument belongs > to the code for the function and thus should be executed at call time. > Not at definion time. Just as other expressions in the function are > not evaluated at definition time. > Yes you could argue for that, but I think it would lead to a more complex and confusing language.
The 'why' is probably at least partly historical. When default arguments were added to Python there were no bound variables, so the option of delaying the evaluation simply wasn't there. However, I'm sure that if default arguments were being added today, and there was a choice between using closures or evaluating the defaults at function definition time, the choice would still come down on the side of simplicity and clarity. (Actually, I think it isn't true that Python today could support evaluating default arguments inside the function without further changes to how it works: currently class variables aren't in scope inside methods so you would need to add support for that at the very least.) If you want the expressions to use closures then you can do that by putting expressions inside the function. If you changed default arguments to make them work in the same way, then you would have to play a lot more games with factory functions. Most of the tricks you can play of the x=x default argument variety are just tricks, but sometimes they can be very useful tricks. -- http://mail.python.org/mailman/listinfo/python-list