2

I am working with code that exhibits the following behavior. Suppose I have a variable d and I assign it to an integer 9

d = 9

why do the following statements work?

In [95]: if d:
   ....:     print d
   ....: else:
   ....:     print 'Did not print d!'
   ....:     
9

In [96]: 

when d itself is not a Boolean and does not pass the following tests:

In [96]: d is True
Out[96]: False

In [97]: d is False
Out[97]: False

I will appreciate it if someone could explain this to me and point out any misconception that I have. Thank you very much.

AKKO
  • 973
  • 2
  • 10
  • 18
  • see this post http://stackoverflow.com/questions/2764017/is-false-0-and-true-1-in-python-an-implementation-detail-or-is-it-guarante – ghostbust555 Jan 09 '15 at 03:20

4 Answers4

4

Quoting from the official Python documentation,

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 if statement falls under the context of Booelan operations. Since d being 9 doesn't fall under any of the false values, it is considered as Truthy.

You can confirm the same like this

>>> all(bool(value) is False for value in (False, 0, +0, -0, "", (), [], {}, set()))
True

Though the value 9 is neither True nor False, which you determined with d is True and d is False tests, it is considered as Truthy because of the above mentioned reason.

>>> bool(9)
True
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
3

Remember, is tests for identity. Your d is True and d is False statements are returning False because d is 9, not True or False. However, the truthy value of d is True - try running bool(d) to get its Boolean value (hint: it'll return True).

As stated elsewhere, False, None, 0, and empty strings, lists, dicts, sets, tuples, etc. are all considered falsy. Objects can also be falsy depending on the contents of their __nonzero__ and/or __len__ attributes, if set.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
1

In your example, the integer is treated as a boolean within the if statement. Any non-zero integer will be equivalent to True. This is common in many programming languages.

bryc
  • 12,710
  • 6
  • 41
  • 61
-1

By design, Python evaluates most values to TRUE. Only a very small set are considered FALSE.

This ends up being a very nice feature when we are doing things like testing for empty values, or iterating over conditional loops.

See section 5.1 in the docs!

https://docs.python.org/2/library/stdtypes.html

andrew
  • 3,929
  • 1
  • 25
  • 38
  • it has nothing to do with "erroring" on the side of anything - non-zero integers are truthy. – MattDMo Jan 09 '15 at 03:25
  • I removed the idiom "to error on the side of". I can see that it might be confusing. Otherwise, I don't know why you are down voting this answer. – andrew Jan 09 '15 at 03:51