liveBook · Manning

Runtime check

As explained in Software Design for Python Programmers

A runtime check is a conditional inspection of values or types performed while a program is executing.

A runtime check conditionally inspects arguments, values, or types while a program executes. It can select behavior for overloaded methods or reject invalid data. section 7.1.3 section 2.3.4

Common uses include:

  • Check argument count and datatypes to make one method support several equivalent operations. section 7.1.3
  • Check parameter values before constructing objects, preventing invalid Attributes instances. section 2.3.4
  • Replace scattered type checks when a more flexible data model can encapsulate validation and matching. section 2.3.4

Selecting an operation at runtime

Shape.area(arg1, arg2=None) calculates a circle’s area when arg2 is absent and a rectangle’s area otherwise. Line.length similarly accepts coordinate arguments, sequences, dictionaries, or Point objects, then reports unsupported arguments. section 7.1.3

Listing 7.5 (Program 7.3 Shape) shape.py
import math

class Shape:
    def area(self, arg1, arg2=None):
        return (math.pi*arg1*arg1 if arg2 is None
                                  else arg1*arg2
               ) 

Inspect the compact conditional implementation of runtime-selected area calculations. section 7.1.3

How Software Design for Python Programmers covers it

How deep it goes
Demonstrates argument-count, datatype, and value checks, then contrasts them with a design that removes redundant type inspection.
Examples
Uses Shape.area, Line.length, Point objects, book attributes, and catalogue searches as concrete validation and dispatch examples.
What it assumes
Assumes basic Python methods, classes, parameters, and datatypes while teaching runtime dispatch through small domain examples.
What you can do after
Builds Python methods that dispatch on supported argument forms and validates values before creating domain objects.
How it connects
Connects runtime checks to method overloading, defensive programming, encapsulation, and the Don't Repeat Yourself Principle.

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.