Default Parameters to function!

Timothy Docker timd at macquarie.com.au
Wed Mar 15 23:25:52 CET 2000


Michael Hudson <mwh21 at cam.ac.uk> writes:

> "Yuriy Gorvitovskiy" <yuriy at centricsoftware.com> writes:
> 
> > I want to make a request to the Python developers to change the way of
> > default parameters initialization from "when function definition is
> > executed" to "when function executed". 
> 
> Loosely speaking, no chance.  Too much code relies on this behaviour
> (yes, that's perhaps a bed idea, but it's still true).

How much code really relies on this behaviour? The normal workaround
for what people really want is as the manual suggests, ie:

    def f( x=None ):
	if x == None:
	    x = []

This would continue to work if defaults were evaluated at
function application time. How much code is written like

    def f( x=[] ):
	x.append( 'vvv' )

and expects to get the mutated value next time a default is needed?
Not too much I suspect.

> What do you want to do instead?  Store little code objects for each
> default argument and evaluate them each time?  I guess efficiency was
> the initial reason for this implementation.

If the "code objects" for defaults were only evaluated as required,
it's hard to see that things would be any slower that the first code
snippet above (which evals code when a default is required).

Is there any reason why this shouldn't be considered for python
3000? Just about everyone hits this sooner or later, suggesting that
that the current behaviour is not the ideal one.

Tim



More information about the Python-list mailing list