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


Groups > comp.lang.python > #90138

Re: Is this unpythonic?

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'list?': 0.07; 'advice.': 0.09; 'agree,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'wrong,': 0.09; 'def': 0.12; 'wrote': 0.14; 'carefully.': 0.16; "chris'": 0.16; 'cleaner': 0.16; 'code?': 0.16; 'gotcha.': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 50': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'replacing': 0.19; 'thanks.': 0.20; 'code,': 0.22; "aren't": 0.24; 'looks': 0.24; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'leave': 0.29; 'is?': 0.30; 'work.': 0.31; 'code': 0.31; 'fri,': 0.33; 'could': 0.34; 'but': 0.35; 'thanks': 0.36; 'subject:?': 0.36; 'wrong': 0.37; 'list': 0.37; 'e.g.': 0.38; 'to:addr:python- list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'little': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'read': 0.60; 'more': 0.64; 'situation': 0.65; 'frank': 0.68; 'subject:this': 0.83; '2015': 0.84; 'joel': 0.91; 'convinced': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From "Frank Millman" <frank@chagford.com>
Subject Re: Is this unpythonic?
Date Fri, 8 May 2015 12:53:19 +0200
References <mailman.224.1431072121.12865.python-list@python.org> <554c8b0a$0$12992$c3e8da3$5496439d@news.astraweb.com>
X-Gmane-NNTP-Posting-Host 197.86.223.132
X-MSMail-Priority Normal
X-Newsreader Microsoft Outlook Express 6.00.3790.4657
X-MimeOLE Produced By Microsoft MimeOLE V6.00.3790.4913
X-RFC2646 Format=Flowed; Original
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.231.1431082398.12865.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1431082398 news.xs4all.nl 2865 [2001:888:2000:d::a6]:56591
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:90138

Show key headers only | View raw


"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 | NextPrevious in thread | Next 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