missing 'xor' Boolean operator

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu Jul 16 01:37:02 EDT 2009

On Wed, 15 Jul 2009 21:05:16 +0200, Jean-Michel Pichavant wrote:

> Didn't know that.
> So if I resume:
> - not 'foo' => False
> - 'foo' or 'foo' => 'foo'
> I may be missing something, but honestly, Guido must have smoked some
> heavy stuff to write such logic, has he ?

No, it's perfectly reasonable, and not at all the product of mind-
altering drugs. Other languages do the same thing.

for x in alist or blist or clist:
    print x

will select the first non-empty list and iterate over that.

Every object in Python has a truth value. By convention, we say that 
objects are Something or Nothing.

0 None [] '' are examples of Nothing, or false values.

1 2.5 [x, y, z] 'foo' are examples of Something, or true values.

Note the distinction between lower-case "false" and "true" (adjectives), 
and title-case False and True (nouns).

`if x: ... else:` branches according to whether x is Something or 
Nothing, not whether it is True or False.

The operators `and` and `or` also return Something or Nothing, short-
circuiting as appropriate.


More information about the Python-list mailing list