Best way to set/get an object property

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue Aug 26 10:03:35 EDT 2008


Steven D'Aprano a écrit :
(snip)
> But it's quite rare to see double-underscore "really private" attributes 
> in Python code. It is considered to go against the spirit of the language.

Not necessarily "against the spirit" - it's mostly than __name_mangling 
is only really useful when you want to protect a really vital 
implementation attribute from being *accidentaly* overridden, and mostly 
annoying anywhere else.

> I'm told that in Java it is quite difficult to change a class from using 
> public attributes to getters/setters,

That's an understatement. Java has *no* support for computed attributes, 
so you just can *not* turn a public attribute into a computed one.

> and therefore many Java developers 
> prefer to use getters/setters right from the beginning.

Truth is that they have no other choice if they want to be able to 
decouple implementation from interface.




More information about the Python-list mailing list