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
