What values are considered false?

Remco Gerlich scarblac at pino.selwerd.nl
Thu Feb 21 09:03:31 CET 2002

Jason <caljason76 at yahoo.com> wrote in comp.lang.python:
> In my experimentation 0, None, "", [], and () all seem to be
> considered false.  Just to confound me none of them are == to each
> other.  Coming from a Scheme world this is confusing.  Are there any
> other values that are false?

{}, at least. Any classes that return the right results on certain special
functions (__nonzero__(), if I remember correctly).

Why is it normal to you that loads of values are true when they're not equal
to each other, but only one can be false? Python isn't Scheme.

> Why aren't () and "" the same thing?

Because one is an empty tuple and one is an empty string.

> Don't both describe an immutable sequence of values (basically, why
> aren't strings either lists or tuples)?

That would be purity. In Python, practicality beats purity. Tuples are
pretty heavy structures, every element has a reference to an object. A
string is just a row of bytes. Python has to interface with C code and files
a lot, and both in C and in typical files, strings are just rows of bytes.

There are other reasons - to me, they're not at all the same thing
conceptually. Tuples nest, strings don't. For a string a="x", a[0] == a.
You can't do that well and still have normal nested tuples, I think.

To pre-empt your next question: watch out, lists aren't like Lisp list.
They're not cons pairs, they're just an array of references in memory.

Remco Gerlich

More information about the Python-list mailing list