Visibility Rules
Medium
+3 pts
Modules & Crates
2/4
🎯 In Python, encapsulation is by convention. You use a leading underscore to signal "private," but anyone can still access it:
class Sensor:
def __init__(self, name, value):
self.name = name
self._value = value # "private" — but not really
@property
def value(self):
return self._value
s = Sensor("temp", 72)
s._value = -999 # oops — nothing stops this
Python's @property gives you a getter, but there's no enforcement. A determined caller can always reach in and modify _value directly.
Rust enforces privacy at compile time. If a field isn't pub, code outside the module literally cannot access it — the compiler rejects it.
Struct field visibility
In Rust, struct fields have their …
Login to see the full exercise.
Topics