Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90125

Re: Is this unpythonic?

References <mihqhb$r1k$1@ger.gmane.org>
Date 2015-05-08 18:25 +1000
Subject Re: Is this unpythonic?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.225.1431073517.12865.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, May 8, 2015 at 6:01 PM, Frank Millman <frank@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Is this unpythonic? Chris Angelico <rosuav@gmail.com> - 2015-05-08 18:25 +1000

csiph-web