Is there some way to ask Python how it evaluates an expression?
Yes, this can be done by trying different parts of the expression
individually, then using the results for further evaluation.
Here is an example from the text:
1 | 9>1e-4 and {0:"Cavern", 5:'Tunnel'}[0].upper() in 'cave'
|
Rather than going through the entire expression according to operator priorities, one can try different subexpressions, seeing how they evaluate. Here, by stepping through various experiments, we see how Python behaves.
print ( {0:"Cavern", 5:'Tunnel'}[0] ) |
print ( {0:"Cavern", 5:'Tunnel'}[0].upper() ) |
print ( 'CAVERN' in 'cave' ) |
print ( True and 'CAVERN' ) |
print ( False and 'CAVERN' ) |
The last two expressions in the stepper-example are puzzling;
the and
operator is intended to be used
between booleans, not between a boolean and a string.
Yet Python tolerates this mixed type usage and produces
a value. Experiments like this answer "what if" questions.