Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!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; 'type,': 0.07; '*name*': 0.09; 'myself,': 0.09; 'object.': 0.09; 'sun,': 0.09; '>>>': 0.12; 'received:74.125.82.174': 0.12; 'received:mail- wy0-f174.google.com': 0.12; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; '(err': 0.16; 'boolean': 0.16; 'constructor.': 0.16; 'continue;': 0.16; 'err': 0.16; 'finney': 0.16; 'subject:() ': 0.16; 'subject:function': 0.16; 'besides': 0.19; 'header:In-Reply- To:1': 0.22; 'curious': 0.23; 'subject:code': 0.23; 'calling': 0.25; 'checked': 0.25; 'equivalent': 0.26; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'daniel': 0.29; "won't": 0.30; '17,': 0.31; 'break;': 0.31; 'goto': 0.31; 'fact': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; "i've": 0.33; 'expression': 0.33; "isn't": 0.34; 'uses': 0.34; 'question': 0.35; 'function.': 0.35; 'rather': 0.36; 'else': 0.37; 'case': 0.37; '8bit%:12': 0.38; 'apr': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'pretty': 0.38; 'sources': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'necessarily': 0.40; 'best': 0.60; 'charset:windows-1252': 0.61; '2011': 0.62; 'no:': 0.91; 'technically': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=JAD81pBp0r3fXpe/YVe6jUor93OK2ien2QzqgcqwE34=; b=DbyxRBD5A3EUdfATLwDWrD1TnMHMo0ILW1LZjuZuQd3iiTVqoKkq360iNuJcXDt8Nx qI4HvHzu6AcKec97+5Ff8aGiY32IWLFNQQo5e+rLQI5UooPlkUC7XODrUj7fN8Uhp2A3 GiHZmsRbnXzHXtAFoCrP/c0Ke4cjdR9+HdG5o= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=cMvdRyXcK+olwrycfx7sg0GYHsfUOeWmWe6enrQ4aO39DBWX/VWFrqx4bZM15TrWZ5 539/5sxYTl7IgRyWofxkQirDDOSyS4gm5D4vZ7BrojHRhDJ4kgg+LFZX4ne+sYltdnkT v/McoFfM1RSt+xD1DVG125/mT7mXnTB2tbE3Q= MIME-Version: 1.0 In-Reply-To: <87k4etho6e.fsf@benfinney.id.au> References: <4da9fb0b$0$13696$426a74cc@news.free.fr> <87k4etho6e.fsf@benfinney.id.au> Date: Mon, 18 Apr 2011 10:45:16 +1100 Subject: Re: Equivalent code to the bool() built-in function From: Daniel Kluev To: python-list@python.org Content-Type: text/plain; charset=windows-1252 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: 53 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303083918 news.xs4all.nl 81481 [::ffff:82.94.164.166]:60871 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3437 On Sun, Apr 17, 2011 at 8:38 AM, Ben Finney wr= ote: > It won't look up the *name* =91bool=92, but it will use that object. Any > boolean expression is going to be calling the built-in =91bool=92 type > constructor. > > So the answer to the OP's question is no: the function isn't equivalent > to the type, because the OP's =91bool_equivalent=92 function necessarily > uses the built-in =91bool=92 type, while the reverse is not true. Actually, as I was curious myself, I've checked sources and found that `True if x else False` will _not_ call bool(), it calls PyObject_IsTrue() pretty much directly. >>> import dis >>> def bool2(x): ... return True if x else False ... >>> dis.dis(bool2) 2 0 LOAD_FAST 0 (x) 3 POP_JUMP_IF_FALSE 10 6 LOAD_GLOBAL 0 (True) 9 RETURN_VALUE >> 10 LOAD_GLOBAL 1 (False) 13 RETURN_VALUE case POP_JUMP_IF_FALSE: w =3D POP(); if (w =3D=3D Py_True) { Py_DECREF(w); goto fast_next_opcode; } if (w =3D=3D Py_False) { Py_DECREF(w); JUMPTO(oparg); goto fast_next_opcode; } err =3D PyObject_IsTrue(w); Py_DECREF(w); if (err > 0) err =3D 0; else if (err =3D=3D 0) JUMPTO(oparg); else break; continue; So technically these implementations are equivalent besides the fact that bool() is type rather than function. --=20 With best regards, Daniel Kluev