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


Groups > comp.lang.python > #90123

Is this unpythonic?

From "Frank Millman" <frank@chagford.com>
Subject Is this unpythonic?
Date 2015-05-08 10:01 +0200
Newsgroups comp.lang.python
Message-ID <mailman.224.1431072121.12865.python-list@python.org> (permalink)

Show all headers | View raw


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


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


Thread

Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-08 10:01 +0200
  Re: Is this unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-08 20:08 +1000
    Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-08 12:53 +0200
      Re: Is this unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-09 01:07 +1000
        Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-09 07:56 +0200
        Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-09 08:51 +0200
          Re: Is this unpythonic? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-05-09 19:47 +1200
            Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-09 11:10 +0200
    Re: Is this unpythonic? Dave Angel <davea@davea.name> - 2015-05-08 08:04 -0400
      Re: Is this unpythonic? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-05-10 10:04 +0200
        Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-10 10:58 +0200
          Re: Is this unpythonic? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-05-10 11:54 +0200
            Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-10 12:40 +0200
    Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-08 15:12 +0200

csiph-web