Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90138
| From | "Frank Millman" <frank@chagford.com> |
|---|---|
| Subject | Re: Is this unpythonic? |
| Date | 2015-05-08 12:53 +0200 |
| References | <mailman.224.1431072121.12865.python-list@python.org> <554c8b0a$0$12992$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.231.1431082398.12865.python-list@python.org> (permalink) |
"Steven D'Aprano" <steve+comp.lang.python@pearwood.info> wrote in message news:554c8b0a$0$12992$c3e8da3$5496439d@news.astraweb.com... > On Fri, 8 May 2015 06:01 pm, Frank Millman wrote: > >> Hi all >> [...] >> >> 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. > > It is a gotcha, and a code smell. > > http://www.joelonsoftware.com/articles/Wrong.html > Interesting read - thanks. > You can use it, but with care: code smells aren't necessarily wrong, they > just need to be looked at a little more carefully. > > >> 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? > > I would change it to z=None *unless* you actually required the list to be > mutated, e.g. if you were using it as a cache. > Ok, you and Joel have convinced me. I will change it to z=None. > Does z have to be a list? Could you use an empty tuple instead? > > def x(y, z=()): ... > That was Chris' suggestion as well (thanks Chris). The idea appealed to me, but then I found a situation where I pass in a dictionary instead of a list, so that would not work. Replacing them all with None is cleaner and, I now agree, more pythonic. Thanks for the good advice. Frank
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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