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

Erik Max Francis max at alcyone.com
Sun Apr 11 23:29:23 EDT 2004


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 && max at alcyone.com && 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



More information about the Python-list mailing list