Python - Using or to select one non empty value


You can select from a set of objects with an or.

A statement such as this:

X = A or B or C or None 

assigns X to the first nonempty (that is, true) object among A, B, and C, or to None if all of them are empty.

This works because the or operator returns one of its two objects.

You can use this form to get the default value.

The following sets X to A if A is true (or nonempty), and to default otherwise:

X = A or default 

Related Topic