Is this unpythonic?

Chris Angelico rosuav at gmail.com
Fri May 8 04:25:14 EDT 2015


On Fri, May 8, 2015 at 6:01 PM, Frank Millman <frank at chagford.com> wrote:
> I have often read about the gotcha regarding 'mutable default arguments'
> that frequently trips people up.
>
> I use them from time to time, but I have never had a problem. I have just
> delved a bit deeper to see if I am skating on thin ice.
>
> AFAICT my usage is safe. If I use a list as an argument, I only use it to
> pass values *into* the function, but I never modify the list. So if I omit
> the argument, the original default empty list is used, otherwise the list
> that I pass in is used.

In that case, you may want to consider using a default of () rather
than [], unless there's some reason that it has to be a list. With a
tuple, you have a guarantee that it's not going to be changed, so it's
as safe as a default argument of True or 42 or "Hello".

That may or may not help, but it's worth considering, especially if
all you really need is an iterable (or a sequence).

ChrisA



More information about the Python-list mailing list