Is this unpythonic?
Frank Millman
frank at chagford.com
Fri May 8 04:01:52 EDT 2015
Hi all
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.
However, every time I look at my own code, and I see "def x(y, z=[]):
....." it looks wrong because I have been conditioned to think of it as a
gotcha.
Would it be more pythonic to change them all to use the alternative
"z=None", or is it ok to leave it as it is? Or to phrase it differently, how
would an experienced pythonista react on seeing this when reviewing my code?
Thanks
Frank Millman
More information about the Python-list
mailing list