Should Python raise a warning for mutable default arguments?

Lie Lie.1296 at gmail.com
Sat Aug 23 17:22:29 CEST 2008


On Aug 22, 10:32 pm, Peter Otten <__pete... at web.de> wrote:
> Christian Heimes 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?
>
> -0 from me. I'd rather feature it more prominently in the tutorial, a
> section "The five most common pitfalls" or something like that.
>
> > I don't see a chance for your proposal. How are you going to detect
> > mutable objects? Custom types can be mutable as well as immutable.
>
> A check at compilation time for list literals would catch 90 % of the cases.
> The warning would be targeted at newbies after all. It might still be a
> source of confusion when they try to import someone else's code that uses
> mutable defaults intentionally.

-1. I think, as many people before me has said, we should treat
programmers as an adult[1], and doesn't try to babysit them by giving
a warning, or worse as an exception. But I do agree that python should
have something like -Wall, where programmers are warned of all
potential problems.

[1] Anyway, you spent your time being a newbie only for a very short
time.



More information about the Python-list mailing list