Checkpoint

Checkpoint: Fuel Flasks

Lantern fuel is carried in flasks. The quartermaster wants flasks that behave like real Python values:

a, b = Flask(3), Flask(4)
a + b                  # Flask(7)
sorted([b, a])         # [Flask(3), Flask(4)]
Flask(-1)              # ValueError: litres can't be negative

This checkpoint combines the chapter's tools: dunder methods, @total_ordering and a descriptor that checks values.

Assignment

  1. Finish the NonNegative descriptor. __get__ returns the value stored on the instance (or the descriptor itself when obj is None). __set__ raises ValueError(f"{self.public} can't be negative") when value < 0, otherwise stores it.
  2. Give Flask a __repr__ like Flask(7).
  3. a + b returns a new Flask holding both amounts. Return NotImplemented if b isn't a Flask.
  4. Add __eq__ and __lt__ (comparing litres, NotImplemented for non-flasks) and decorate the class with @total_ordering.