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

Discussion in 'Python' started by Vineet Jain, Apr 12, 2004.

  1. Vineet Jain

    Vineet Jain Guest

    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

    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??

    I still think int('2.1') should return 2. If the programmer is using int
    then he should know that what he will get out of the result is an int
    regardless of what comes in. Especially since int accepts a float object in
    any case. It seems a waste of resources to have to first convert a string to
    a float object and then convert it to an int object.

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

    VJ

    -----Original Message-----
    From: Jeff Epler [mailto:]
    Sent: Sunday, April 11, 2004 5:35 PM
    To: Vineet Jain
    Cc:
    Subject: Re: Is there a boolean(somestring) equivalent of
    int(somestring). bool('false') -> True


    The reason that
    >>> bool('false')

    returns True is the same reason that
    >>> not 'false'

    is False: A non-empty string, when treated as a boolean, is true.

    Changing the meaning of bool() is not likely to happen.

    Here's one piece of code you might try to get the behavior you desire:

    true_values = '1 yes true on'.split()
    false_values = '0 no false off'.split()
    def parse_boolean_value(s):
    if s in true_values: return True
    if s in false_values: return False
    raise ValueError

    'i in seq' is just like checking whether any item in seq is equal to i.
    If seq is a dictionary or a set (or defines __contains__), this test can
    be a little more efficient, but the efficiency is unlikely to matter
    when there are only 4 items.

    Jeff
    Vineet Jain, Apr 12, 2004
    #1
    1. Advertising

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

    Vineet Jain wrote:

    > How can anyone argue that
    >
    > bool('false') = True
    >
    > is more natural (and what you would expect) than
    >
    > bool('false') = False


    Because at some point in programming you (I mean the general "you," not
    you specifically) actually have to hit the books and learn what
    functions do. A natural language that works simply and reasonably is
    always a good thing, but that's not a _substitute_ for learning, it's an
    assistant.

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


    No, because bool doesn't do this. bool tests whether or not the
    argument passed in is true according to the Python language, and returns
    True if so; otherwise it returns False. This is not the behavior you
    want; you want to test whether or not the argument is a string which
    looks like it might represent the concept of true. That's for a
    function you might call doesStringLookTrue, but not the bool
    function/type.

    After all, are you disturbed by this:

    if 'false':
    print "This should never happen since the test is false"

    executes the "then" statement? If you are, then you don't understand
    Python's concept of Booleans. If you aren't, then you're trying to
    _introduce_ an inconsistency, not solve one, because

    if x: ...

    should always behave the same as

    if bool(x): ...

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ Whoever named it necking was a poor judge of anatomy.
    -- Groucho Marx
    Erik Max Francis, Apr 12, 2004
    #2
    1. Advertising

  3. Vineet Jain

    Mel Wilson Guest

    Re: Is there a boolean(somestring) equivalent of int(somestring).

    In article <>,
    "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




    bool("False") == True

    because "False" isn't a null string.


    eval ("False") == False

    because False is the name of the object named False, and
    eval exists in order to take strings and execute them as
    Python code, which includes referring to named objects.

    What did "Vineet Jain" have for lunch?

    Regards. Mel.
    Mel Wilson, Apr 12, 2004
    #3
    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:
    462
    Andreas Klemt
    Jul 4, 2003
  2. Paul Prescod
    Replies:
    1
    Views:
    444
    Joe Mason
    Apr 12, 2004
  3. André
    Replies:
    3
    Views:
    1,563
  4. Gary Wessle

    cout true for bool(true)

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

Share This Page