Accessors in Python (getters and setters)

Ant antroy at gmail.com
Mon Jul 10 09:12:23 EDT 2006


mystilleef wrote:
> I decided to change the name of an attribute. Problem is I've used the
> attribute in several places spanning thousands of lines of code. If I
> had encapsulated the attribute via an accessor, I wouldn't need to do
> an unreliable and tedious search and replace accross several source
> code files to achieve my goal. I could simply change the name of the
> attribute and move on.

You could, but then you'd be left with crap names for your accessors!
In your equivalent Java code, you'd typically have used the accessors
in several places throughout the code (or else why bother using them?),
so you wouldn't be any better off!

The main benefit for having accessors in Java is not that you can
change the *name* of an attribute, but that you can change the
implementation of the attribute - i.e. change the what actually happens
to  when the accessor is called. Which you can do in Python with
properties.




More information about the Python-list mailing list