Should Python raise a warning for mutable default arguments?

Steven D'Aprano steve at
Sat Aug 23 02:35:29 CEST 2008

On Fri, 22 Aug 2008 11:14:09 -0700, Emile van Sebille wrote:

> Steven D'Aprano wrote:

>> I suggest that Python should raise warnings.RuntimeWarning (or
>> similar?) when a function is defined with a default argument consisting
>> of a list, dict or set. (This is not meant as an exhaustive list of all
>> possible mutable types, but as the most common ones that I expect will
>> trip up newbies.) The warning should refer to the relevant FAQ or
>> section in the docs.
>> What do people think?
> -1
> People that have worked through the tutorial, something everyone should
> do when they're starting out, 

I never worked through the Python tutorial. I bought a book. By the time 
I even knew the tutorial existed, I had finished the book and spent six 
months programming in Python and the tutorial was far too basic for me. 
I'm sure there are thousands of other Python developers who have had 
similar experiences. You can't expect every beginner's book on Python 
programming to feature a discussion on mutable defaults.

Perhaps Python should print a warning when you start up: "If you haven't 
worked through the tutorial, you are not allowed to ask questions about 
surprising behaviour".

> will find this explicitly discussed.  See
> People that just skim the surface get stung -- sorry.

You assume that people will read the tutorial and then immediately trip 
over a real life example. There's a lot of stuff in the tutorial and it 
doesn't all stick the first time you read it. Just recently, perhaps a 
week or so ago, we had a case of a fellow who had been programming in 
Python for many years before he stumbled across this behaviour. (I forget 
the name of the thread, but I'm sure you can find it.) It's not just 
noobs who trip over this.


More information about the Python-list mailing list