Re: Is there a boolean(somestring) equivalent of int(somestring).bool('false') -> True

Discussion in 'Python' started by Paul Prescod, Apr 12, 2004.

  1. Paul Prescod

    Paul Prescod Guest

    Vineet Jain wrote:

    > Thanks for the code suggestion.
    >
    > From what I've read, most people (including me) are attracted to Python
    > because of how natural it is. Give that is a high priority for the language
    > designers:
    >
    > How can anyone argue that
    >
    > bool('false') = True
    >
    > is more natural (and what you would expect) than
    >
    > bool('false') = False


    For most (all?) built-in types there is one and only one false value. If
    you start to accept multiple false values then you run into the types of
    problems Perl programmers run into. You write code like this:

    a = input()
    if a:
    ...
    else:
    ...

    And knowing what it does requires you to keep in mind ALL of the false
    values for the type. So you test your program with one dataset and one
    day another of the false values pops up and your program behaves
    incorrectly. Python's rule is simpler.

    > Having just got over the surprise that int('2.1') fails while
    > int(float('2.1')) works the above surprises me even more. If the int case
    > was rejected because it is 'better to be explicit than not' then how can you
    > bool('false') be allowed??


    "false" is just a random English word. The Python builtin is called
    "False".

    > Booleans are associated with (True, on, 1 and yes) and bool() on any them
    > should return True.


    Maybe in your mind. What about "true", "TRUE", "oui", "1.0", "positive"
    and "yeah". How far do you intend to go? Name a single language or
    system that treats "True", "on", "1" and "yes" as positive values and
    "False", "off", "0" and "no" as negative values. If Python started
    guessing what strings are vaguely affirmative, people would always
    "expect" it to read their minds just a little bit more effectively than
    it does. The current rule is very simple: the empty string is false. All
    others are true. It is trivially easy for you to redefine conversion to
    boolean for any particular data set:

    def mybool():
    return mystring=="oui"

    Paul Prescod
     
    Paul Prescod, Apr 12, 2004
    #1
    1. Advertising

  2. Paul Prescod

    Joe Mason Guest

    Re: Is there a boolean(somestring) equivalent of int(somestring). bool('false') -> True

    In article <>, Paul Prescod wrote:
    > Maybe in your mind. What about "true", "TRUE", "oui", "1.0", "positive"
    > and "yeah". How far do you intend to go? Name a single language or
    > system that treats "True", "on", "1" and "yes" as positive values and
    > "False", "off", "0" and "no" as negative values. If Python started


    UniConf

    http://open.nit.ca/wiki/?page=UniConf

    (The current list is "true", "yes", "on", and "enabled" evaluate to 1,
    "false", "no", "off", and "disabled" to 0, anything else is passed
    through strtol. This is specifically a configuration system, where it
    makes sense to be forgiving in what you accept, but you did ask for a
    language *or system*.)

    Joe
     
    Joe Mason, Apr 12, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    0
    Views:
    475
    Andreas Klemt
    Jul 4, 2003
  2. Vineet Jain
    Replies:
    2
    Views:
    344
    Mel Wilson
    Apr 12, 2004
  3. André
    Replies:
    3
    Views:
    1,596
  4. Gary Wessle

    cout true for bool(true)

    Gary Wessle, Nov 2, 2006, in forum: C++
    Replies:
    17
    Views:
    823
    peter koch
    Nov 4, 2006
  5. bdb112
    Replies:
    45
    Views:
    1,348
    jazbees
    Apr 29, 2009
Loading...

Share This Page