[Python-ideas] Default arguments in Python - the return

Terry Reedy tjreedy at udel.edu
Sat May 9 01:34:42 CEST 2009


Pascal Chambon wrote:

> I'm surely not original in any way there, but I'd like to put back on 
> the table the matter of "default argument values".

There have been two proposals:
1. Evaluate the expression once, store the result, and copy on each 
function call.
- Expensive.
- Nearly always not needed.
- Not always possible.
2. Store the expression and evaluate on each function call (your 
re-proposal).
- Expensive.
- The result may be different for each function call, and might raise an 
exception.
- This is the job of the suite !!!!!!!!!!!!!!!!
- Which is to say, run-time code belongs in the function body, not the 
header.

> And no one seemed to enjoy the possibilities of getting "potentially 
> static variables" this way.

You did not search hard enough.

> Static variables are imo a rather bad idea, 

So you want to take them away from everyone else.  I think *that* is a 
rather bad idea ;-).  No one is forcing you to use them.

> On the other hand, when people write "def func(mylist=[]):", they 
> basically DO want a fresh new list at each call,

Maybe, maybe not.

> be it given by the caller or the default argument system.
> So it's really a pity to need tricks like
>  >/ def f(a, L=None):
> />/ if L is None:
> />/ L = []

Or don't supply a default arg if that is not what you really want.
Putting call time code in the function body is not a trick.

> /to get what we want (and if None was also a possible value ? 

__none = object()
def(par = __none):
   if par == __none: ...

as had been posted each time this question has been asked.

> I guess this will mean some overhead during function call,

I absolutely guarantee that this will.  Functions calls are expensive. 
Adding a function call for each default arg (and many functions have 
more than one) multiplies the calling overhead.

 > so this might become another issue.

Is and always has been.

Terry Jan Reedy





More information about the Python-ideas mailing list