Default Value

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jun 21 21:31:35 EDT 2013


On Sat, 22 Jun 2013 05:07:59 +1000, Chris Angelico wrote:

> Oh! I know. Function argument defaults will now be restricted to
> int/float/tuple. That would do it, right? Nobody would be bothered by
> little restrictions like that, would they.

Alas, tuples won't do it. Fortunately, you can include str and bytes 
(unicode and str) and frozenset in the "Good List". Tuples have to go 
into the "Bad List" because, although they themselves are immutable, 
their contents may not be. Imagine the confusion and horror that poor 
developers will experience when they do something like this:

def func(arg, extra=(23, 'foo', [])):
    [code goes here...]
    extra[2].append("something")
    [more code...]


and they've now mutated the immutable default!

Thinking about this, I think that the only safe thing to do in Rickython 
4000 is to prohibit putting mutable objects inside tuples. Putting a list 
or a dict inside a tuple is just a bug waiting to happen!

-- 
Steven



More information about the Python-list mailing list