[Tutor] default argument frustration
alan.gauld at freenet.co.uk
Fri Feb 11 19:30:17 CET 2005
> FOR THE LOVE OF MIKE can someone tell me even one reason why this
> isn't a misfeature?!?!
Its the only sane way to implement default arguments. The whole
point of function definitions is that they provide a single concise
interface. The function should return the same result each time
you call it with the same input. The only way to achieve that
is to have the default calculated once.
The problem is that the default value can be a mutable object
(and that aspect could be argued as a mis-feature) which leads
to apparently different behaviour for the same input as the
recent thread illustrated. But in fact the same object is being
returnd its just the internal content that changes...
And of course this behaviour is exploited regularly in Python
in the same way that static variables are exploited in C.
But I agree that it can be confusing when the default is mutable
but since the only reasonable option would have been to restrict
default values to immutable types I think I prefer the confusion...
More information about the Tutor