bool constructor is inconsistent?

S

Stefan Schwarzer

Hi Neal,

IN [3]: bool('False')
Out[3]: True

If you consider strings, only an empty string has a false
value. So the string 'False' which is non-empty, results in
a true boolean value.

For example, you can use

if my_string:
...

to execute some code if the string is not empty.

Stefan
 
D

David Robinow

Hi Neal,

IN [3]: bool('False')
Out[3]: True

If you consider strings, only an empty string has a false
value. So the string 'False' which is non-empty, results in
a true boolean value.
...
I've always felt that if a humorous post needs a smiley, that it's not funny.
However, there is the risk of being misunderstood.
 
S

Steven D'Aprano

IN [3]: bool('False')
Out[3]: True

In [4]: int('32')
Out[4]: 32

Where is the inconsistency? bool('False') returns the same result as for
any other non-empty string:
True



Treating the string "False" as identical to the named global False would
be inconsistent.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top