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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'things.': 0.05; 'exception.': 0.07; 'false.': 0.07; '"if': 0.09; 'bool': 0.09; 'function:': 0.09; 'name)': 0.09; "object's": 0.09; 'semantics': 0.09; 'subject:while': 0.09; 'to:addr:comp.lang.python': 0.09; 'type)': 0.09; 'useless': 0.09; 'cc:addr:python-list': 0.10; '"""if': 0.16; '"if"': 0.16; 'evaluates': 0.16; 'hasattr(obj,': 0.16; 'interpreting': 0.16; 'silly': 0.16; 'syntax,': 0.16; 'wrote:': 0.17; 'handles': 0.18; 'explicit': 0.22; 'implicit': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'programming': 0.23; '15,': 0.23; 'statement': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'testing': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'prevent': 0.27; 'rules': 0.27; 'measure': 0.29; 'obj': 0.29; 'wrap': 0.29; 'no,': 0.29; 'points': 0.29; 'probably': 0.29; "we're": 0.30; 'function': 0.30; 'resolution': 0.30; 'code': 0.31; 'point': 0.31; 'not.': 0.32; 'values.': 0.33; 'equal': 0.33; 'received:google.com': 0.34; 'false': 0.35; 'faster': 0.35; 'doing': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'anything': 0.36; 'test': 0.36; 'should': 0.36; 'bad': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'some': 0.38; 'things': 0.38; 'instead': 0.39; 'easily': 0.39; 'where': 0.40; 'your': 0.60; 'july': 0.60; 'back': 0.62; 'our': 0.65; 'equals': 0.65; 'skill': 0.65; 'started.': 0.65; 'presented': 0.72; 'truth': 0.75; 'forced': 0.84; 'majority.': 0.84; 'ridiculously': 0.84; 'why?': 0.84; 'received:209.85.213.184': 0.91; 'received:mail-yx0-f184.google.com': 0.91; 'remember,': 0.93; 'yes!': 0.93 Newsgroups: comp.lang.python Date: Sun, 15 Jul 2012 11:56:49 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=166.249.198.115; posting-account=h3aEwQoAAACiuqX-oR3gvCVFm8lLHoWj References: <5002a1f9$0$29995$c3e8da3$5496439d@news.astraweb.com> <7b027612-a07e-40f9-8ad2-3e95c5440482@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 166.249.198.115 MIME-Version: 1.0 Subject: Re: Implicit conversion to boolean in if and while statements From: Rick Johnson To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: , Message-ID: Lines: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342378613 news.xs4all.nl 6932 [2001:888:2000:d::a6]:52354 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25366 On Sunday, July 15, 2012 1:01:58 PM UTC-5, Ian wrote: > 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? Because we are keeping the condition consistent. We are not relying on impl= icit resolution of an object's value based on some dark, esoteric and incon= sistent rules that defy all normal logic. > What should "if" do if presented a value that isn't True or False? > Raise a TypeError? YES! Because IT IS the author's responsibility to present a condition that = evaluates to either True or False. Anything else would be ridiculously inco= nsistent. > 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))". =20 We cannot prevent morons from doing stupid things. "x=3D=3D7" IS an explici= t statement that evaluates to either True or False. Likewise, isinstance(ob= j, type) is a function that evaluates to either True or False. Wrapping eit= her example in a bool call is redundant and only obfuscates the meaning. Tr= ue equals True and False equal False. Why do you need to test that truth? The only time you will be forced to use the bool is when you are NOT using = rich comparisons or NOT using truth testing functions in a condition. The f= ollowing require NO bool function: obj =3D=3D obj -> bool obj !=3D obj -> bool obj > obj -> bool obj < obj -> bool obj >=3D obj -> bool obj <=3D obj -> bool isinstance(obj, type) -> bool callable(obj) -> bool hasattr(obj, name) -> bool issubclass(obj, name) -> bool ..along with any function that returns a bool Whereas:=20 "if obj" -> Some esoteric semantics that defies all logic! > 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. =20 No, because we want our code to be EXPLICIT and consistent! Remember, writi= ng obfuscated code is easy, however, interpreting obfuscated code is diffic= ult! A good measure of your programming skill is to see how easily your cod= e is read by the majority.=20 """If it's difficult to explain, it's probably a bad idea""".=20 "if blah" is difficult to explain, whereas "if bool(blah)" is not. =20 > At the point that happens, the "bool()" is > effectively just part of the if syntax, and we're back to where we > started. That's a ridiculous conclusion. See points above^^^