Grade 10 Computer Science · Objects
What to keep inside
The boundary between what the object promises and how it does it.
Nobody is breaking in, so why hide anything?
Nobody is trying to break your code, and Python does not block access to any field at all. And still programmers mark fields as internal and ask others not to touch them, not even just to read. What exactly are they guarding against?
Every object has two parts: what may be touched from outside, and what belongs to it alone. This boundary is not protection against malice — no one is trying to break your code. It is protection against reliance: the moment someone outside touches an internal field, that field becomes part of the promise, and it can no longer be changed.
class Temperature:
def __init__(self, celsius):
self._celsius = celsius # internal
def celsius(self):
return self._celsius
def fahrenheit(self):
return self._celsius * 9 / 5 + 32What the boundary buys
In the example only one Celsius value is kept, and Fahrenheit is computed at each call. One could have chosen the reverse, or kept both. As long as no one outside touches the internal field, that decision stays yours forever — and if converting at each call turns out too expensive, you can keep both without a single line of outside code knowing anything happened.
| What is visible | What is internal |
|---|---|
| What the object promises | How it keeps it |
| Hard to change | Easy to change |
| People rely on it | No one knows about it |
Example 1 — a decision left open
- Only one unit is kept.
- The second is computed at each call.
- This can be swapped without breaking anyone.
Example 2 — reliance that closes options
- Outside code reads the internal field.
- Now it is part of the promise.
- And any change to it breaks code.