Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38408

Re: Implicit conversion to boolean in if and while statements

Newsgroups comp.lang.python
Date 2013-02-07 21:53 -0800
References <mailman.2132.1342341291.4697.python-list@python.org> <5002a1f9$0$29995$c3e8da3$5496439d@news.astraweb.com> <mailman.2141.1342369188.4697.python-list@python.org> <7b027612-a07e-40f9-8ad2-3e95c5440482@googlegroups.com> <f2b3fa89-d977-4ffd-8861-6a49fcbaf66c@g4g2000pbn.googlegroups.com>
Message-ID <67531902-cccc-4966-8278-b9948818dbde@googlegroups.com> (permalink)
Subject Re: Implicit conversion to boolean in if and while statements
From Rick Johnson <rantingrickjohnson@gmail.com>

Show all headers | View raw


On Monday, July 16, 2012 8:45:51 PM UTC-5, rusi wrote:
> On Jul 15, 9:50 pm, Rick Johnson <rantingrickjohn...@gmail.com> wrote:

> > I think this issue is not so much a "bool test" vs "type
> > test", but more an ambiguous syntax issue.
> > 
> 
> If you know some English, its clear that if and while
> create bool contexts. 

Wrong. "if and "while" do not /create/ anything. On a syntactical level they merely /suggest/ to the reader that the following statement is expected to be a boolean value. It is the /statement/ itself that creates the boolean value, not the keywords! 

Observe:

    0 == 0 -> True
    isinstance("5", int) -> False
  
You see, "if" and "while" don't create anything, in reality they merely execute a block of code depending on the value of the statement that follows the keyword. "if" and "while" are only *logical switches* and nothing more. You could write a simple quasi-example of "if" as a function like this:

    def if_(value):
        if not value:
            return
        # do_something_here

  
Those previous statements where /explicit/, and as such need no bool() function to resolve their Boolean values, however, consider the following /implicit/ conversions to Boolean:

    [] -> False
    [1] -> True
    "" -> False
    "1" -> True
    0 -> False
    1 -> True
    2 -> True
    etc...
  
It is my strong opinion that these types of implicit conversions are evil obfuscations of the truth. If we want to convert an object to a Boolean, then use the bool() function on that object:

    bool([]) -> False
    bool([1]) -> True
    etc...
  
Heck! Why even have a damn bool function if you're never going to use it?
  
> [If you know English but have not
> studied logic the 'if/while' make sense whereas 'bool' is
> gobbledygook]

And which Univeristy would you recommend for studying the intricacies of "gobbledygook"? ;-)

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Implicit conversion to boolean in if and while statements Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-07 21:53 -0800
  Re: Implicit conversion to boolean in if and while statements Chris Angelico <rosuav@gmail.com> - 2013-02-08 17:15 +1100
  Re: Implicit conversion to boolean in if and while statements Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-08 18:22 +1100
    Re: Implicit conversion to boolean in if and while statements MRAB <python@mrabarnett.plus.com> - 2013-02-08 17:14 +0000
      Re: Implicit conversion to boolean in if and while statements Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-09 09:18 +1100
  Re: Implicit conversion to boolean in if and while statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-02-08 09:42 +0000

csiph-web