Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!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; 'python.': 0.02; 'explicitly': 0.04; '"if': 0.09; 'definition,': 0.09; 'does,': 0.09; 'forcing': 0.09; 'if,': 0.09; 'subject:while': 0.09; 'cc:addr:python-list': 0.10; "wouldn't": 0.11; 'interfaces': 0.15; 'stack': 0.15; 'angle': 0.16; 'boolean': 0.16; 'conditional': 0.16; 'container.': 0.16; 'dist': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'typing': 0.17; 'obviously': 0.18; 'code.': 0.20; 'do.': 0.21; 'converted': 0.22; 'tells': 0.22; 'cc:2**0': 0.23; '15,': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'chris': 0.28; 'obj': 0.29; 'convert': 0.29; 'objects': 0.29; 'figure': 0.30; 'generally': 0.32; 'received:209.85.160.46': 0.32; 'could': 0.32; "can't": 0.34; 'agree': 0.34; 'received:google.com': 0.34; 'so,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'should': 0.36; 'being': 0.37; 'why': 0.37; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'instead': 0.39; 'header:Received:5': 0.40; 'help': 0.40; 'different': 0.63; 'more': 0.63; 'jul': 0.65; 'lose': 0.71; 'inform': 0.78; 'forced': 0.84; 'spinning,': 0.84; 'thing,': 0.84; 'rick': 0.91; 'reducing': 0.95 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 :cc:content-type; bh=MEZEVP7O58OY1tF4ialsaunY9j/wAzHzPvzflpphWDs=; b=gb+k0+dSEUkIsyomoQwwCH9gzJ+kn/qdx5qfkkBRQTOoZ8J8mfbNHPd7TKI//Dt/wz Qf0/aYBIAli8wwkQS6q3SnNacOLCwYDnhtUo02LJezLAmOWMZh77LrFi98yfLDz92wvA GLgXV1l/3KVBI+jGDsaHT+J/GF0k+sw1UWZar0aaq2uTYO9ArFOiVRK2fp7V3fiGnrly 45W1RLa8IIhAdiOkPjqGkDX6cvVAOOYvAiPHkDzBfF0nIVphx5JrLjsEwnUjiNa4Pe++ KlBVdgX0KWZNox9Ymq21YXmgBH+MfjJyu50R77VvYbM6gUvGJlYpMewx4kvj0b4lS87X Ek/g== MIME-Version: 1.0 In-Reply-To: References: <5002a1f9$0$29995$c3e8da3$5496439d@news.astraweb.com> <7b027612-a07e-40f9-8ad2-3e95c5440482@googlegroups.com> <86872ad2-fda0-403b-9f18-d1cb18e41860@t32g2000yqd.googlegroups.com> From: Devin Jeanpierre Date: Sun, 15 Jul 2012 22:15:13 -0400 Subject: Re: Implicit conversion to boolean in if and while statements To: Chris Angelico Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org 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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342404957 news.xs4all.nl 6887 [2001:888:2000:d::a6]:52925 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25382 On Sun, Jul 15, 2012 at 9:51 PM, Chris Angelico wrote: >> if bool(obj) and a==b: # Correct! >> if obj and a==b: # Incorrect! > > That still doesn't answer the question of what bool(obj) should do if > obj is not a bool, and why if can't do the exact same thing, since if, > by definition, is looking for a boolean state selector. If can obviously do the exact same thing -- it does, in Python. I don't agree with the angle that Rick is spinning, so let me write my own: By forcing the objects in conditional to be booleans, you are forced to do something to non-booleans to convert them. By doing so, you will help inform the reader what the non-boolean is, which makes it easier for them to figure out the code. For example, instead of "if stack:" or "if bool(stack):", we could use "if stack.isempty():". This line tells us explicitly that stack is a container. Or instead of "if dist:" or "if bool(dist):" we could use "if dist == 0:". This tells us explicitly that stack is a number. Supposedly this makes it easier to read code. It certainly reads more like English! :) As far as I know, the only use of having a polymorphic boolean conversion is reducing the amount of typing we do. Generally objects with otherwise different interfaces are not interchangeable just because they can be converted to booleans, so you wouldn't lose much by being forced to explicitly convert to boolean with something interface-specific. -- Devin