Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'argument': 0.05; 'modify': 0.07; 'odd': 0.07; 'explanation': 0.09; 'promising': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'wrote': 0.14; 'mutable': 0.16; 'reasonably': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'thereby': 0.16; 'tuple': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'all,': 0.19; '>>>': 0.22; 'instead.': 0.24; 'define': 0.26; 'somewhere': 0.26; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'testing': 0.29; 'code': 0.31; 'globally': 0.31; 'overhead': 0.31; 'comment': 0.34; 'problem': 0.35; 'point.': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'subject:?': 0.36; 'should': 0.36; 'too': 0.37; 'list': 0.37; 'list.': 0.37; 'step': 0.37; 'to:addr:python-list': 0.38; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'easy': 0.60; 'dave': 0.60; 'simple': 0.61; "you're": 0.61; 'show': 0.63; 'became': 0.64; 'situation': 0.65; 'frank': 0.68; 'nobody': 0.68; 'reads': 0.68; 'default': 0.69; 'safe': 0.72; 'subject:this': 0.83; 'angel': 0.91; 'technique': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Is this unpythonic? Date: Sun, 10 May 2015 10:58:44 +0200 References: <554c8b0a$0$12992$c3e8da3$5496439d@news.astraweb.com> X-Gmane-NNTP-Posting-Host: 197.89.67.74 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431248365 news.xs4all.nl 2917 [2001:888:2000:d::a6]:57791 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90277 "Johannes Bauer" wrote in message news:min3f0$2gh$1@news.albasani.net... On 08.05.2015 14:04, Dave Angel wrote: > > It might be appropriate to define the list at top-level, as > > > > EMPTY_LIST=[] > > > > and in your default argument as > > def x(y, z=EMPTY_LIST): > > > > and with the all-caps, you're thereby promising that nobody will modify > > that list. > I think it's a really bad idea to use a module-global mutable > "EMPTY_LIST". It's much too easy this happens: > # Globally > >>> EMPTY_LIST = [ ] > # At somewhere in the code at some point in time > >>> foo = EMPTY_LIST > >>> foo.append(123) > >>> print(foo) > [123] > # Some other place in code > >>> bar = EMPTY_LIST > >>> print(bar) > [123] A fair point. How about this as an alternative? If one were to use this technique at all, it would be necessary to add a comment at the top explaining the reason for this odd declaration. It is then a simple extra step to say - EMPTY_L:IST = () and if required - EMPTY_DICT = () and expand the explanation to show why a tuple is used instead. So if there was a situation where the overhead of testing for None became a problem, this solution offers the following - 1. it solves the 'overhead' problem 2. it reads reasonably intuitively in the body of the program 3. it is safe 4. it should not be difficult to write a suitable self-explanatory comment Frank