Missing Values, Defaults, and None
None and Optional Types
BOTIE
That notation did not decide what absence should mean for every application. A caller might omit a limit to request a default, or explicitly supply None to disable an optional check. Keeping those states distinct prevents a convenience expression from silently changing the policy.
Sources for this history
Did This Greenhouse Disable Its Alert or Forget the Setting?
Most greenhouses use the default alert limit. One greenhouse supplies zero as its limit, and another supplies None to disable the alert while its thermometer is replaced. A helper uses value or default, so it replaces both settings with the default.
The values were different, but the helper treated every false-like value as the same situation. We need to distinguish an absent key from a supplied value before choosing what a default should mean.
The workshop displays Python's None as null. It assumes the supplied numerical settings are valid; the Python examples also explain how to validate them and distinguish an omitted function argument.
Step 1 of 1
Resolve the setting before comparing
The example default limit is 25 Celsius. Zero is a supplied limit; None disables the check.
| Record setting | Resolved limit | Reason |
|---|---|---|
| limit key absent | 25 | use the default |
| limit: 0 | 0 | preserve the explicit number |
| limit: None | None | preserve the explicit disabled state |
limit key absent
- Resolved limit
- 25
- Reason
- use the default
limit: 0
- Resolved limit
- 0
- Reason
- preserve the explicit number
limit: None
- Resolved limit
- None
- Reason
- preserve the explicit disabled state
Distinguish zero from absence
The default fills an absent setting. It does not override a supplied numerical limit or turn a disabled alert back on.
Keep Missing, Zero, and Disabled Settings Separate
North has no alert limit field, so it uses the shared default of 25 degrees Celsius. South supplies its own limit of 28. Each greenhouse needs to keep the setting it requested when the default changes.
BOTIE
Alert status in greenhouse order
- 0below limit
- 1alert
Follow the operations
1. Greenhouse readings and alert settings
- house
- north
- temperature_c
- 21
- limit
- Not provided
- house
- south
- temperature_c
- 30
- limit
- 28
house temperature_c limit north 21 Not provided south 30 28 2. Read temperatures in Celsius
- 021
- 130
3. Shared default limit in Celsius
25
4. Fill only missing alert limits
- 025
- 128
5. Check each greenhouse's alert
- 0below limit
- 1alert
6. Result
- 0below limit
- 1alert
The workshop displays Python's None as null. It assumes the supplied numerical settings are valid; the Python examples also explain how to validate them and distinguish an omitted function argument.
1. Distinguish zero from absence
Zero can be a real measurement or setting. None is a separate Python object commonly used for an absent value. Test value is None when that is the state you mean.
An expression such as value or default tests truthiness. Zero, False, an empty string and an empty list are false-like, so this expression is inappropriate when those are legitimate supplied values.
Choose a test that matches the application state rather than grouping unrelated values together.
Preserve an explicit zero
Compare an absence check with a truthiness fallback.
limit_c = 0
default_c = 25
print(limit_c or default_c)
print(default_c if limit_c is None else limit_c)25
0The second expression preserves zero because it asks specifically about None.
2. Make missing keys different from present keys
A dictionary can omit a key entirely. If a key exists with None as its value, that is a present entry carrying a particular value. The in operator checks key presence.
Dictionary get with a default uses that default for an absent key. It does not replace a present None. Decide whether your application wants to preserve that distinction before using a fallback.
Resolve the setting first, then apply its documented behavior.
Inspect all three states
Each record describes a different configuration choice.
records = [{}, {"limit": 0}, {"limit": None}]
print([row.get("limit", 25) for row in records])
print(["limit" in row for row in records])[25, 0, None]
[False, True, True]A missing key uses the default, while both explicitly supplied entries remain intact.
Check yourself
Questions on Parts 1 to 2
Question 1 of 2
Under a missing-key-only default, what limit applies?
Question data
default_c = 25
record = {"limit": 0}3. Separate nullable values from optional arguments
float | None says that a value may be a number or None. It does not by itself allow the caller to omit the argument. An argument becomes omittable when its signature gives it a default.
Within a function, check for None before performing operations that require a number. A static checker can use that branch to narrow the possible type, and the runtime branch gives the absence state its intended behavior.
The signature describes permitted values and call syntax. The function body implements their meaning.
Give None a stated result
A disabled limit produces no alert, while an explicit zero remains active.
def reaches_optional_limit(value: float, limit: float | None) -> bool:
if limit is None:
return False
return value >= limit
print(reaches_optional_limit(0, 0))
print(reaches_optional_limit(100, None))True
FalseNone follows a separate branch before the numerical comparison.
4. Use a sentinel when None is a real request
A function may need to distinguish an omitted argument from an explicit None. Create one private sentinel object and use identity to recognize it. A new object created at each comparison would not be the same sentinel.
In an update operation, omission can mean leave the setting unchanged, while None means clear it. A sentinel preserves both choices without reserving a number that might later be valid.
The private marker is an implementation detail, so callers use omission rather than passing it themselves.
Recognize omission separately
This small function reports the caller’s choice without printing an unstable object representation.
_UNSET = object()
def describe_change(value=_UNSET):
if value is _UNSET:
return "unchanged"
if value is None:
return "disabled"
return "supplied"
print(describe_change())
print(describe_change(None))
print(describe_change(0))unchanged
disabled
suppliedOmission, explicit None and zero reach different branches.
5. Validate the values that remain
Preserving distinctions does not mean accepting every supplied object. This application permits finite numerical limits or None. A string and a boolean are invalid settings, even though either can be present in a dictionary.
Return a new configuration when the interface promises an update without mutation. Retain unrelated fields rather than replacing the whole record with one setting.
The challenges check the default path, explicit disable, valid zero and invalid supplied values.
Check yourself
Questions on Parts 1 to 5
Question 1 of 4
Does this signature allow check() with no argument?
Question data
def check(limit: float | None): ...Try it with Botie
Build with Botie
Write and run Python