Should Python raise a warning for mutable default arguments?

Peter Otten __peter__ at web.de
Fri Aug 22 11:55:13 EDT 2008


bearophileHUGS at lycos.com 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 tmp.py
def test(x, a=[]):
    a.append(x)
    return a

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

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

Warnings...

tmp.py:2: Modifying parameter (a) with a default value may have unexpected
consequences

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

Peter



More information about the Python-list mailing list