Should Python raise a warning for mutable default arguments?

Paddy paddy3118 at googlemail.com
Sat Aug 23 00:01:15 EDT 2008


On Aug 22, 4:55 pm, Peter Otten <__pete... at web.de> wrote:
> bearophileH... 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

+1 on this.

It seems an obvious think to add to a lint-like tool rather than
burdening core Python.

- Paddy.



More information about the Python-list mailing list