Should Python raise a warning for mutable default arguments?

Peter Otten __peter__ at
Fri Aug 22 17:55:13 CEST 2008

bearophileHUGS at wrote:

> DrScheme is an implementation of Scheme that is very newbie-friendly.
> It has several limited sub-languages, etc.
> So maybe a command line option can be added to Python3 ( -
> newbie ? :-) ) that just switches on similar warnings, to help newbies
> (in schools, where there's a teacher that encourages to always use
> that command line option) avoid some of the most common traps.

Or maybe bundle pychecker with idle?

$ cat
def test(x, a=[]):
    return a

for i in range(5):
    print test(i)

$ pychecker
Processing tmp...
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]

Warnings... Modifying parameter (a) with a default value may have unexpected

Though it might be interesting to ask a newbie what he expects when warned
of "unexpected consequences" ;)


More information about the Python-list mailing list