Expressions and Statements

A readable program makes it clear which constructs compute values and which steps change the state or direction of the computation.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
2 min
In this article4 chapters
  1. Follow the value
  2. The missing assignment
  3. Follow a result through its enclosing statement
  4. Work through a value that appears to disappear

Follow the value

An expression can appear where a value is expected. In fee = 7 + rate * units, the arithmetic expression produces the value assigned to fee. A statement supplies a step in the enclosing block. Python permits certain expressions as standalone statements, so log(fee) can be evaluated and its result discarded. Evaluating a call may also cause side effects.

The missing assignment

Suppose total starts at ten. Evaluating total + 1 as a line in a script leaves the binding at ten. In an interactive interpreter the same expression usually displays eleven. The statement total = total + 1 computes eleven and then changes the binding. When reproducing a surprising result, copy the assignment as well as the expression into your execution trace.[1][2]

Follow a result through its enclosing statement

In fee = subtotal(items) * tax_factor, evaluating subtotal(items) produces one value and reading tax_factor supplies another. Multiplication combines them. Assignment associates fee with the result after that computation completes. If the call fails, the outer assignment never receives a result. The same rule helps with an index assignment such as fees[order_id] = fee. The right-hand expression supplies a value, and executing the assignment updates a position in the mapping.

In Python, a call can be used wherever an expression of its result type makes sense. An expression statement such as items.append("tea") is useful for its effect. It still has a result, in this case None. Assigning that result back to items changes the binding to None after the append. The list was updated successfully, and the next action fails because the name now refers to something else. The contract of a mutating function explains both observations.

Work through a value that appears to disappear

boxes = [3, 5]
copy = sorted(boxes, reverse=True)
boxes.sort()
assert copy == [5, 3]
assert boxes == [3, 5]
assert boxes.sort() is None

In this example, sorted constructs a list and returns it, so assignment can preserve that result. sort changes its receiver and supplies no new list. Use an unsorted fixture such as [5, 3] if you also want to observe in-place rearrangement clearly. To check a line of your own code, write down the expression's ordinary result and every object it can change. A return-value trace is particularly helpful when a language's interactive console displays results automatically.

References

  1. Python Software Foundation, Python Language Reference: Execution Model ↩
  2. Python Software Foundation, Python Language Reference: Expressions ↩