liveBook · Manning

Assertion

As explained in Python How-To

A software assertion is an executable check of an expected condition or property that signals failure when the check does not hold.

A Python assertion checks a condition: execution continues when it is true, and raises an AssertionError when it is false. Unlike an expression, an assertion statement checks a condition rather than producing a value. section 2.1.2 section 2.1.3

Assertions can compare a function’s result with an expected value. For instances of a custom class, comparing their __dict__ representations can serve as a proxy for comparing the instances. section 13.3.1

assert create_task("Laundry,3").__dict__ == Task("Laundry", 3).__dict__

This assertion checks that create_task returns an instance with the expected attributes. section 13.3.1

How Python How-To covers it

How deep it goes
Explains condition outcomes and demonstrates equality checks for a function result.
Examples
Uses f-string output comparisons and a task-creation function compared with an expected Task instance.
What you can do after
Apply it via Python's assert statement to check conditions and compare a function's result with an expected value.
How it connects
Connects assertions to Boolean conditions, expressions versus statements, f-strings, and function testing.

Also explained in

Compare how 15 books explain assertion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.