3 ms·
The difference between getters (or properties) and attributes (or fields) is that getters are a real function and attribute access does not use a function. The
by rovolo 4y ago
The difference between getters (or properties) and attributes (or fields) is that getters are a real function and attribute access does not use a function. The attribute access is less work because you don't have to do a method dispatch to get the value. (At least historically; the method dispatch may be optimized out now for basic getters.)
Most of the time your getters will just return the attribute, but there are situations where you don't have an attribute backing your property:
1) You're renaming the field, and you can't update all uses at once. You can create a synthetic property with the old name which delegates to the new name.
2) You want to lazily compute the property only when requested.
3) You want to perform a side-effect every time the field is accessed