Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.03; 'attribute': 0.05; 'exception.': 0.07; 'function,': 0.07; 'means,': 0.07; '"if': 0.09; 'bool': 0.09; 'subject:while': 0.09; 'useless': 0.09; '"if"': 0.16; '*must*': 0.16; '*never*': 0.16; 'silly': 0.16; 'syntax,': 0.16; 'syntax.': 0.16; 'workings': 0.16; 'wrote:': 0.17; 'handles': 0.18; 'issue.': 0.20; 'interpret': 0.22; 'defined': 0.22; 'example': 0.23; '15,': 0.23; 'statement': 0.23; 'this:': 0.23; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'implies': 0.29; 'lies': 0.29; 'wrap': 0.29; "we're": 0.30; 'point': 0.31; 'johnson': 0.32; 'values.': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; 'received:google.com': 0.34; 'faster': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'but': 0.36; 'should': 0.36; 'too': 0.36; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'some': 0.38; 'things': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'first': 0.61; 'back': 0.62; 'more': 0.63; 'jul': 0.65; 'started.': 0.65; 'presented': 0.72; 'money': 0.74; 'why?': 0.84; 'rick': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=AZDAYx5z8SuGisrGh4lDprYQ5VrSZ5VPkgUu85i3mk8=; b=d+9CpiMZKZb9RanjSIJ8a9X07es16SBHK1zPksfKh1LN3HgNclPO/mCMF2CNb74q7M 1RSSNYbSCKQ8QJndQXhLFs1QMahY8X1nbP3fHP7IO8oDikZ/KvRY6mAOLbZa2PCUWscg mVsLWn76s/RwwozQgniZtS0hxRBL6CssRhw3gM9EZdUFEO/z7kIit9iARjpZJj0F5uIi C4two89AET06YFqP4LKDYkADcrMzL50207cA0yQrLqombgfA65974ZEU577dG22tBSqx DHlYjNHJBIkbdhnmym4+eBBcZ8e2JgDMa3xh5viVpyVW1i8IpBEEKrPSu8QLWZGNx2/3 YEww== MIME-Version: 1.0 In-Reply-To: <7b027612-a07e-40f9-8ad2-3e95c5440482@googlegroups.com> References: <5002a1f9$0$29995$c3e8da3$5496439d@news.astraweb.com> <7b027612-a07e-40f9-8ad2-3e95c5440482@googlegroups.com> From: Ian Kelly Date: Sun, 15 Jul 2012 12:01:58 -0600 Subject: Re: Implicit conversion to boolean in if and while statements To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342375350 news.xs4all.nl 6940 [2001:888:2000:d::a6]:45305 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25364 On Sun, Jul 15, 2012 at 10:50 AM, Rick Johnson wrote: > I think this issue is not so much a "bool test" vs "type test", but more = an ambiguous syntax issue. Consider this: > > ## EXAMPLE A ## > py> if money: > ... do_something() > > The syntax "if money" implies we are testing/measuring some attribute of = "money", but what exactly about money are we testing/measuring? The problem= lies in the syntax. To understand this syntax, we must first interpret wha= t *IF* means, and we should *NEVER* need to interpret such a well defined w= ord as *IF*! This syntax is far too opaque. Consider the alternative: > > ## EXAMPLE B ## > py> if bool(money): > ... do_something() > > Now we have a hint. Even if we don't understand the inner workings of the= "bool" function, we *do* understand that the statement "bool(money)" *must= * return True or the block *will not* execute. So now instead of having to understand how "if" handles arbitrary values, we have to understand how "bool" handles arbitrary values. How is that an improvement? What should "if" do if presented a value that isn't True or False? Raise a TypeError? Next thing we know, people get so used to wrapping everything they present to "if" in a "bool()" call, that they start writing silly things like "if bool(x =3D=3D 7)" and "if bool(isinstance(x, int))". Why? Because it's faster and easier to automatically wrap the value in "bool" than it is to put in the effort to verify that the value will always be a bool to begin with in order to avoid a useless and annoying exception. At the point that happens, the "bool()" is effectively just part of the if syntax, and we're back to where we started.