conceptual problem (was: A beginning beginner's question about input, output and . . .

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Jan 15 00:32:35 EST 2021


On 14/01/21 11:49 am, Cameron Simpson wrote:
> The "pure" OOP approach, where method calls are used as messages to set
> or fetch aspects of the object, is usually does with getter and setter
> methods like:
> 
>      x = o.getX()
>      o.setX(9)

People use get and set methods, not because it's somehow morally
wrong to expose attributes directly, but as a workaround for the
lack of a language feature.

In C++ and Java, different calling syntax is required for direct
access and access mediated by methods, so if you start out exposing
something directly and then change your mind, all the code using it
has to be changed. For this reason, people got into the habit of
wrapping everything in get and set methods from the beginning,
"just in case".

Python doesn't have this problem -- you can turn an attribute
into a property at any time, and nothing else needs to change.
So get and set methods are unnecessary and actively discouraged
in Python.

(C#, if I understand correctly, gets this sort of half-right.
You can turn an attribute into a property, and the calling *source*
doesn't change, but it all has to be recompiled -- which kind of
defeats the purpose.)

-- 
Greg


More information about the Python-list mailing list