Values, Types, and Supported Operations

A type describes which operations a value supports and how those operations behave when applied.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Predict the operation
  2. Find the boundary
  3. Trace one calculation from its input boundary
  4. Check which outputs an operation promises

Predict the operation

In Python, 7 + 4 adds numbers and "7" + "4" joins text. Both use the same written operator. A type helps determine what evaluation will do after the operands arrive. Check expressions and statements when studying when the operation runs.

Find the boundary

An inventory parser receiving "7" should decide whether the field is a stored label or a quantity. Parsing it once gives later calculations numerical behavior. Use parsing and formatting to place conversion at the interface and account for malformed text.[1][2]

Trace one calculation from its input boundary

Imagine a CSV export containing fields for item code and count. The row 007,12 arrives as text. The code stays text because its zero padding identifies a particular item, while the count becomes the number twelve. Multiplying the code by two in Python would repeat the text, giving 007007. Multiplying the count by two gives twenty-four. Both operations succeed, so catching exceptions would not detect a type mix-up. A typed record gives these fields separate names and makes their meanings available to the calculation.

Check which outputs an operation promises

The allowed input type should tell the reader which operations will be meaningful. For a duration helper receiving two values, state whether both arrive in seconds and whether it returns a number or a formatted label. Then test a fractional number such as 1.5 and a text form such as "1.5" at the actual input boundary. The former may be a valid numeric input, while the latter needs conversion. Once the values share the intended representation, downstream functions can rely on that part of the contract without repeating the same conversion at each call.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩
  2. Python Software Foundation, Python Language Reference: Expressions ↩