liveBook · Manning

Postcondition

As explained in Software Design for Python Programmers

A postcondition is a state or Boolean condition that must be true when a function or operation returns.

A postcondition is a Boolean condition that must be true when a function or operation returns. When its precondition was satisfied, the function is responsible for establishing the postcondition before returning. section 6.3.3

Use postconditions to make successful behavior explicit:

  • After add() succeeds on a non-full circular buffer, the buffer contains the new value and is not empty: self._count > 0. section 6.3.3
  • After remove() succeeds on a non-empty circular buffer, it returns a value and the buffer is not full: self._count < self._capacity. section 6.3.3
  • If the operation cannot establish its promised result, raising an exception changes the contract to successful completion or an exception. section 6.3.3

A contract for a circular buffer

A caller first confirms that add() or remove() can legally run. The method then performs its operation and checks the resulting state before returning. A failed check may abort in a demonstration; an application can instead raise an exception. This separates the caller’s precondition responsibility from the function’s postcondition responsibility. section 6.3 section 6.3.3

Postconditions make method behavior predictable. Together with preconditions and a class invariant, they form a programming-by-contract description that helps prevent objects from being left in invalid states. section 6.3 section 6.4

How Software Design for Python Programmers covers it

How deep it goes
Defines Boolean return conditions, applies them to circular-buffer methods, and notes exceptions as an alternative failure contract.
Examples
Uses a catalogue-search use case and circular-buffer add() and remove() operations as concrete postcondition examples.
What it assumes
Assumes familiarity with functions, callers, methods, and Boolean conditions while relating postconditions to preconditions and class invariants.
What you can do after
Diagnose/debug with it by checking promised states at function return and choosing failure behavior when those states cannot be established.
How it connects
Connects postconditions with preconditions, class invariants, programming by contract, methods, and use case descriptions.

Also explained in

Compare how 2 books explain postcondition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.