57

I tried running this piece of code:

path = '/bla/bla/bla'
if path is True:
 print "True"
else:
 print "False"

And it prints False. I thought Python treats anything with value as True. Why is this happening?

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
asked Feb 25, 2011 at 16:23
0

3 Answers 3

107

is compares identity. A string will never be identical to a not-string.

== is equality. But a string will never be equal to either True or False.

You want neither.

path = '/bla/bla/bla'
if path:
 print "True"
else:
 print "False"
answered Feb 25, 2011 at 16:25
Sign up to request clarification or add additional context in comments.

Comments

69

From 6.11. Boolean operations :

In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.

The key phrasing here that I think you are misunderstanding is "interpreted as false" or "interpreted as true". This does not mean that any of those values are identical to True or False, or even equal to True or False.

The expression '/bla/bla/bla' will be treated as true where a Boolean expression is expected (like in an if statement), but the expressions '/bla/bla/bla' is True and '/bla/bla/bla' == True will evaluate to False for the reasons in Ignacio's answer.

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Feb 25, 2011 at 16:53

1 Comment

+1 I think this is the underlaying misunderstanding. Maybe it helps to know that there is a bool function that "interprets" a value as a boolean. You could say this function is called implicitly in every if (which is probably not true in a technical sense, but at least adding it never changes the behavior of a if statement).
5

While the other posters addressed why is True does what it does, I wanted to respond to this part of your post:

I thought Python treats anything with value as True. Why is this happening?

Coming from Java, I got tripped up by this, too. Python does not treat anything with a value as True. Witness:

if 0:
 print("Won't get here")

This will print nothing because 0 is treated as False. In fact, zero of any numeric type evaluates to False. They also made decimal work the way you'd expect:

from decimal import *
from fractions import *
if 0 or 0.0 or 0j or Decimal(0) or Fraction(0, 1):
 print("Won't get here")

Here are the other value which evaluate to False:

if None or False or '' or () or [] or {} or set() or range(0):
 print("Won't get here")

Sources:

  1. Python Truth Value Testing is Awesome
  2. Truth Value Testing (in Built-in Types)
Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Oct 15, 2018 at 5:15

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.