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: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'defaults': 0.05; 'none:': 0.05; 'advice.': 0.09; 'none.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'bug': 0.10; 'def': 0.14; 'applies': 0.15; 'argument': 0.15; 'value.': 0.15; '"none"': 0.16; 'boolean': 0.16; 'evaluates': 0.16; 'false:': 0.16; 'former,': 0.16; "method's": 0.16; 'operators.': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sentinel': 0.16; 'set,': 0.16; 'singleton': 0.16; 'variable': 0.20; 'compare': 0.20; 'arguments': 0.22; 'explicit': 0.22; 'testing': 0.25; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'checking': 0.27; 'equivalent': 0.27; 'not.': 0.27; '(such': 0.27; 'fine': 0.29; "i'm": 0.29; 'always,': 0.29; 'comparison': 0.29; 'equality': 0.29; 'pep': 0.29; 'no,': 0.29; 'e.g.': 0.31; "i'd": 0.31; 'code': 0.31; 'subject:?': 0.34; 'google': 0.34; 'that,': 0.34; 'could': 0.35; 'to:addr:python- list': 0.35; 'done': 0.35; 'false': 0.35; 'identity': 0.35; 'set.': 0.35; 'really': 0.35; 'but': 0.36; 'possible.': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'instead': 0.38; 'received:org': 0.38; 'say': 0.38; 'doing': 0.38; 'mean': 0.38; 'test': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'some': 0.40; 'better.': 0.66; '_o__)': 0.84; 'container)': 0.84; 'received:125': 0.84; 'russell': 0.84; 'subject:via': 0.84; '\xe2\x80\xa6': 0.84; 'hand,': 0.97 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Argument Presence Checking via Identity or Boolean Operation? Date: Thu, 04 Jun 2015 17:39:56 +1000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:QGIVKnSKgXNw6GwdgGlCVZYyOQc= 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: 1433403620 news.xs4all.nl 2901 [2001:888:2000:d::a6]:40279 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92038 Russell Brennan writes: > I'm going to x-post this to stackoverflow but... > > When checking a method's arguments to see whether they were set, is it > pythonic to do an identity check: > > def doThis(arg1, arg2=None): > if arg2 is None: > arg2 = myClass() That is the Pythonic way to test whether an argument was set. > In support of the former, PEP 8 states: > > Comparisons to singletons like None should always be done with is or > is not , never the equality operators. Also, beware of writing if x > when you really mean if x is not None -- e.g. when testing whether a > variable or argument that defaults to None was set to some other > value. The other value might have a type (such as a container) that > could be false in a boolean context! Yes. That's good advice. > On the other hand, from the Google style guide: > > Use the "implicit" false if at all possible. ... That's an over-statement. I'd say it is a bug in the document; “if not otherwise contradicted by the style guide” is better. What they are likely warning against is code like this:: if foo is False: # … which should instead be written:: if not foo: # … If the guide is not explicit about that, it should be. > But at the same time states... > > Never use == or != to compare singletons like None. Use is or is not. > > > Does this apply to "None" since it evaluates to False always, and/or is a > boolean comparison equivalent to ==/!= under the hood? No, it applies to None because it is a singleton and designed to be a sentinel value. -- \ “A fine is a tax for doing wrong. A tax is a fine for doing | `\ well.” —anonymous | _o__) | Ben Finney