Accessors in Python (getters and setters)

Diez B. Roggisch deets at nospam.web.de
Wed Jul 19 08:55:15 EDT 2006


>> Then why do you advise "(making) all attributes of a class
>> private/protected" and systematically using properties ?
>>
> 
> Because you don't want third parties illegimately tampering with an
> object's internal data and thus crashing your system?

Ah, you mean like in JAVA where the compiler prevents you from accessing
private variables, but the runtime allows access to these very variables
via reflection? 

Or in C++, where the malevolent programmer just removes the private
declarations in his compile run, and accesses the "private" data from your
object as if it was never declared that way?

You better forget thinking about these hilarious mechanisms of access
control as something that enforces security, and start thinking about them
as a convention that states "I told you not to tamper with this, you've
been warned" - which is done in python using a leading underscore. Or two.

Unless setting or getting a property implies code being run beside the
actual state change, there is absolutely no reason to use accessors. But I
fear this horse has been beaten to death in front of you so many times, yet
still you refuse to see it that way - so, go and use accessors if you like.
Nobody cares....


Diez



More information about the Python-list mailing list