Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'none,': 0.05; "'',": 0.07; "'a'": 0.07; '[],': 0.07; 'e.g.,': 0.07; 'false,': 0.07; 'think,': 0.07; 'python': 0.09; '"if': 0.09; 'bool': 0.09; 'false)': 0.09; 'int)': 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; 'spelling': 0.09; 'subject:while': 0.09; 'terry': 0.09; '{},': 0.09; '(like': 0.15; 'cases': 0.15; 'value.': 0.15; '0.0,': 0.16; 'false:': 0.16; 'message- id:@dough.gmane.org': 0.16; 'none"': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'redundant.': 0.16; 'reedy': 0.16; 'sense,': 0.16; 'set()': 0.16; 'should.': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'instance,': 0.17; '(in': 0.18; 'jan': 0.18; '(or': 0.18; 'changes': 0.20; 'do.': 0.21; 'branch': 0.23; 'nearly': 0.23; 'testing': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; 'am,': 0.27; 'possibility': 0.27; 'possibly': 0.27; '(as': 0.27; 'andrew': 0.27; 'have,': 0.27; 'possible,': 0.27; 'header:X-Complaints- To:1': 0.28; "d'aprano": 0.29; 'steven': 0.29; 'expect': 0.31; 'code': 0.31; 'not.': 0.32; 'could': 0.32; 'values.': 0.33; 'to:addr:python-list': 0.33; 'clear': 0.35; 'false': 0.35; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'useful': 0.36; 'should': 0.36; 'execute': 0.37; 'does': 0.37; 'why': 0.37; 'usual': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'object': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'between': 0.63; 'more': 0.63; 'as:': 0.75; 'gotten': 0.75; 'truth': 0.75; 'guaranteed': 0.76; 'etc,': 0.84; 'received:fios.verizon.net': 0.84; 'habit': 0.91; 'mistakes': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Implicit conversion to boolean in if and while statements Date: Sun, 15 Jul 2012 16:28:02 -0400 References: <5002a1f9$0$29995$c3e8da3$5496439d@news.astraweb.com> <5002F7AD.1000209@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 In-Reply-To: <5002F7AD.1000209@gmail.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342384096 news.xs4all.nl 6889 [2001:888:2000:d::a6]:47685 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25373 On 7/15/2012 1:02 PM, Andrew Berg wrote: > On 7/15/2012 5:56 AM, Steven D'Aprano wrote: >> 3) Rather than distinguishing "true" from "false", a more useful >> dichotomy is between "something" and "nothing". Python includes a number >> of ways of spelling "nothing" of various types, such as: >> >> None, 0, 0.0, '', [], {}, set() >> >> and nearly everything else is "something". > Okay, I see the value in this, but I don't understand why None has a > truth value. Because everything does (or should). > I would expect None to mean "doesn't exist" or "unknown" or > something like that - e.g., a value of 0 means 0 jelly beans in the jar > and None means there isn't a jar. > > FWIW, I have, for a reason I forget, gotten into the habit of writing > "if x is not None" when testing for None. If x might possibly be any other false value (as is, I think, the usual case), that is the right thing to do. Even if no other false value is possible, I would still use 'is not None' just to be clear what the false alternative is, and to guard against mistakes (like not knowing that time values can be false) or code changes that add the possibility of ohter false values. > However, I have not been > writing "if x is True: ..."/"elif x is False: ..."/"else: 'ruh-roh'" > when testing for True (in cases where a value of True or False makes > sense, but any other value would not). Should I? If you only want to execute the if branch when x is literally the bool object True and x could be some other non-bool true value such as 1 or 'a' or [1], etc, then you should. If x is guaranteed to be True or False, which is the case you more or less proposed, then you should not. For instance, "if isinstance(x, int) is True:" or "if (n > 3) is True:" are redundant. -- Terry Jan Reedy