Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!feeder5.news.weretis.net!feeder.news-service.com!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'run-time': 0.05; 'implicit': 0.07; '[],': 0.09; 'operator,': 0.09; 'pm,': 0.11; 'def': 0.13; 'received:74.125.82.44': 0.14; 'received:mail- ww0-f44.google.com': 0.14; 'wrote:': 0.14; 'foo,': 0.16; 'lookup': 0.16; 'subject:() ': 0.16; 'subject:function': 0.16; 'ternary': 0.16; '16,': 0.16; 'compiled': 0.18; 'url:blog': 0.18; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'cheers,': 0.20; 'code': 0.22; 'header:In-Reply-To:1': 0.22; '(but': 0.22; 'cc:addr:python-list': 0.22; 'subject:code': 0.23; 'equivalent': 0.26; 'chris': 0.27; 'skip:b 20': 0.27; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'testing': 0.28; 'sat,': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.31; 'skip:- 30': 0.32; '...': 0.32; 'using': 0.34; '"",': 0.35; 'doing': 0.36; 'else': 0.37; 'apr': 0.38; 'received:google.com': 0.38; "it's": 0.40; 'header:Received:5': 0.40; 'give': 0.61; '2011': 0.62; 'candide': 0.84; 'condition,': 0.84; 'max,': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:cc:content-type :content-transfer-encoding; bh=BFbKpYIXZjG45iHNQZQD3bI7NmfoiPvbQ2SBIiEEKXE=; b=fhiws9/98HSNGCx61Q8yudlu8Q9vvDGA9whqyM/wgU6mlzuMBMAPyEXUJAw0X8jtAC AQceMUmwRS4tw0dNizNYvUQel5YglocbRxFFWbu+R7VvCVPTfEsZiB36TolAfP5aU2px NgGiXfMSJAVQuFoYGdU9csLqiOrpunHJYE8DA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:cc:content-type :content-transfer-encoding; b=YQRTj99myzwhgwtw0rQCJizb0aAJ9edWsQm7GGSiq/Ffmn+8YZNvaNaTefOmLtQmek G1K5vwQX4G2exqFTpL0ZOu0KcA7YnYxdEAfbtmrhfqx1hfHx06HLhb2y6hRxYOVzDJuu Ba3jhl7LAJxwUi2QMK98a9D+vF3mR40+aUux8= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <4da9fb0b$0$13696$426a74cc@news.free.fr> References: <4da9fb0b$0$13696$426a74cc@news.free.fr> Date: Sat, 16 Apr 2011 13:58:37 -0700 X-Google-Sender-Auth: cTCeKJ-PDdLdVgeXQAHUpRjJnfo Subject: Re: Equivalent code to the bool() built-in function From: Chris Rebert Cc: python-list@python.org Content-Type: text/plain; charset=UTF-8 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: 38 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1302987519 news.xs4all.nl 81482 [::ffff:82.94.164.166]:48214 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3347 On Sat, Apr 16, 2011 at 1:24 PM, candide wrote: > Consider the following code : > > # -------------------------------------- > def bool_equivalent(x): > =C2=A0 =C2=A0return True if x else False > > > # testing ... > > def foo(x): > =C2=A0 =C2=A0return 10*x > > class C: > =C2=A0 =C2=A0pass > > for x in [42, ("my","baby"), "baobab", max, foo, C] + [None, 0, "", [], > {},()]: > =C2=A0 =C2=A0print bool(x)=3D=3Dbool_equivalent(x) > # -------------------------------------- > > > Is the bool_equivalent() function really equivalent to the bool() built-i= n > function ? The ternary operator, if-statement, and `while` all do the equivalent of an implicit bool() on their condition, so bool_equivalent() will always give the same result as bool() because it's indeed using the moral equivalent of bool() behind the scenes. That is, `True if x else False` conceptually gets compiled down to `True if bool(x) =3D=3D 1 else False` (but without doing a run-time lookup of "bool"). Cheers, Chris -- http://blog.rebertia.com