
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
- Finish the
NonNegativedescriptor.__get__returns the value stored on the instance (or the descriptor itself whenobjisNone).__set__raisesValueError(f"{self.public} can't be negative")whenvalue < 0, otherwise stores it. - Give
Flaska__repr__likeFlask(7). a + breturns a newFlaskholding both amounts. ReturnNotImplementedifbisn't aFlask.- Add
__eq__and__lt__(comparing litres,NotImplementedfor non-flasks) and decorate the class with@total_ordering.
- Finish the
NonNegativedescriptor.__get__returns the value stored on the instance (or the descriptor itself whenobjisNone).__set__raisesValueError(f"{self.public} can't be negative")whenvalue < 0, otherwise stores it. - Give
Flaska__repr__likeFlask(7). a + breturns a newFlaskholding both amounts. ReturnNotImplementedifbisn't aFlask.- Add
__eq__and__lt__(comparing litres,NotImplementedfor non-flasks) and decorate the class with@total_ordering.
main.py Python sleeping
Press Run to try your code, then Check to test it.