[Python-ideas] function defaults and an empty() builtin

Terry Reedy tjreedy at udel.edu
Sun May 22 00:53:14 CEST 2011


On 5/20/2011 2:15 PM, Masklinn wrote:
=
> There are 17 functions or methods with list default parameters and
> 133 with dict default parameters in the Python standard library.

Could you give the re or whatever that you used to find these?
I might want to look and possibly change a few.

>
> Surely some of them legitimately make use of a mutable default
> parameter as some kind of process-wide cache or accumulator, but
> I would doubt the majority does (why would SMTP.sendmail need to
> accumulate data in its mail_options parameter across runs?)

I suspect that that nearly all of these uses are for read-only inputs. 
In Python 3, () could replace [] in such cases, as tuples now have all 
the read-only sequence methods (they once had no methods). Of course, 
even that does not protect against perhaps crazy code like

if input: <mutate it> # skips empty args, default or not

That suggests that all functions that are supposes to only read an input 
sequences should be tested with tuples. Actually, if only an iterable is 
needed, then such should be tested with non-seequence iterables. That is 
actually very easy to produce: for instance, iter((1,2,3)).

Thinking about it more, it the only use of an arg is to iterate through 
key,value pairs, then the default could be 'iter({})' insteaad of '{}' 
to better document the usage.

> Do you know for sure that no mutation of these 150+ parameters will
> ever be introduced, that all of these functions and methods are
> sufficiently tested, called often enough that the introduction of
> a mutation of the default parameter in themselves or one of their
> callees would *never* be able to pass muster?

No. However, anyone qualified for push access to the central source 
should know that defaults should be treated as read-only unless 
documented otherwise. This is especially true for {}. So I consider it a 
somewhat paranoid worry, in the absence of cases where revisers *have* 
introduced mutation where not present before.

That aside, developers have and are improving the test suite. That was 
the focus of the recent post-PyCon sprint. It continues with a test 
improvement most every day. If you want to join us volunteers to improve 
tests further, please do.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list