Online Book Reader

Home Category

Learning Python - Mark Lutz [188]

By Root 1744 0
Python stops at the first true operand it finds. This is usually called short-circuit evaluation, as determining a result short-circuits (terminates) the rest of the expression:

>>> 2 or 3, 3 or 2 # Return left operand if true

(2, 3) # Else, return right operand (true or false)

>>> [] or 3

3

>>> [] or {}

{}

In the first line of the preceding example, both operands (2 and 3) are true (i.e., are nonzero), so Python always stops and returns the one on the left. In the other two tests, the left operand is false (an empty object), so Python simply evaluates and returns the object on the right (which may happen to have either a true or a false value when tested).

and operations also stop as soon as the result is known; however, in this case Python evaluates the operands from left to right and stops at the first false object:

>>> 2 and 3, 3 and 2 # Return left operand if false

(3, 2) # Else, return right operand (true or false)

>>> [] and {}

[]

>>> 3 and []

[]

Here, both operands are true in the first line, so Python evaluates both sides and returns the object on the right. In the second test, the left operand is false ([]), so Python stops and returns it as the test result. In the last test, the left side is true (3), so Python evaluates and returns the object on the right (which happens to be a false []).

The end result of all this is the same as in C and most other languages—you get a value that is logically true or false if tested in an if or while. However, in Python Booleans return either the left or the right object, not a simple integer flag.

This behavior of and and or may seem esoteric at first glance, but see this chapter’s sidebar Why You Will Care: Booleans for examples of how it is sometimes used to advantage in coding by Python programmers. The next section also shows a common way to leverage this behavior, and its replacement in more recent versions of Python.

The if/else Ternary Expression

One common role for the prior section’s Boolean operators is to code an expression that runs the same as an if statement. Consider the following statement, which sets A to either Y or Z, based on the truth value of X:

if X:

A = Y

else:

A = Z

Sometimes, though, the items involved in such a statement are so simple that it seems like overkill to spread them across four lines. At other times, we may want to nest such a construct in a larger statement instead of assigning its result to a variable. For these reasons (and, frankly, because the C language has a similar tool[32]), Python 2.5 introduced a new expression format that allows us to say the same thing in one expression:

A = Y if X else Z

This expression has the exact same effect as the preceding four-line if statement, but it’s simpler to code. As in the statement equivalent, Python runs expression Y only if X turns out to be true, and runs expression Z only if X turns out to be false. That is, it short-circuits, just like the Boolean operators described in the prior section. Here are some examples of it in action:

>>> A = 't' if 'spam' else 'f' # Nonempty is true

>>> A

't'

>>> A = 't' if '' else 'f'

>>> A

'f'

Prior to Python 2.5 (and after 2.5, if you insist), the same effect can often be achieved by a careful combination of the and and or operators, because they return either the object on the left side or the object on the right:

A = ((X and Y) or Z)

This works, but there is a catch—you have to be able to assume that Y will be Boolean true. If that is the case, the effect is the same: the and runs first and returns Y if X is true; if it’s not, the or simply returns Z. In other words, we get “if X then Y else Z.”

This and/or combination also seems to require a “moment of great clarity” to understand the first time you see it, and it’s no longer required as of 2.5—use the equivalent and more robust and mnemonic Y if X else Z instead if you need this as an expression, or use a full if statement if the parts are nontrivial.

As a side note, using the following expression in Python is similar because the bool function will

Return Main Page Previous Page Next Page

®Online Book Reader