Implicit conversion to boolean in if and while statements

Andrew Berg bahamutzero8825 at
Sun Jul 15 19:02:37 CEST 2012

On 7/15/2012 5:56 AM, Steven D'Aprano wrote:
> 3) Rather than distinguishing "true" from "false", a more useful 
> dichotomy is between "something" and "nothing". Python includes a number 
> of ways of spelling "nothing" of various types, such as:
>     None, 0, 0.0, '', [], {}, set()
> and nearly everything else is "something".
Okay, I see the value in this, but I don't understand why None has a
truth value. I would expect None to mean "doesn't exist" or "unknown" or
something like that - e.g., a value of 0 means 0 jelly beans in the jar
and None means there isn't a jar.

FWIW, I have, for a reason I forget, gotten into the habit of writing
"if x is not None" when testing for None. However, I have not been
writing "if x is True: ..."/"elif x is False: ..."/"else: 'ruh-roh'"
when testing for True (in cases where a value of True or False makes
sense, but any other value would not). Should I?

CPython 3.3.0b1 | Windows NT 6.1.7601.17803

More information about the Python-list mailing list