Accessors in Python (getters and setters)

Bruno Desthuilliers onurb at xiludom.gro
Tue Jul 11 07:29:01 EDT 2006


mystilleef wrote:
> Hello,
> 
> Thanks for the responses. The reason I want to change the name of the
> attribute is because it doesn't reflect the purpose of the attribute,
> anymore. The attribute was originally a string object, but not anymore.
> It is primarily a readability issue. There are also a few key
> attributes I don't want developers, including myself, fiddling with.
> Properties /accessors are good because they allow you to encapsulate
> attributes so you can change implementations at will. Some of you have
> argued I would have needed to change accessor names too if I had
> misnamed them earlier. It's hard to say. I find myself changing the
> names of attributes a lot more frequently than I do functions or
> methods. Choosing a crappy attribute name is effortless for me,
> especially during intense coding sessions. I usually realize I've
> choosing a crappy attribute name the next day, sometimes even later.
> However, I put a lot more effort into choosing method and function
> names, especially when I know it may likely be a public API. 

What you need to understand here is that in Python,
1/ methods *are* attributes
2/ every attribute whose name is not prefixed by a leading underscore is
considered part of the api ('__magic__' names being a special case).

So it has nothing to do with "data vs method" dichotomy (which makes no
sens in a languages where functions and methods are objects), only with
"API vs implementation". You choosed a crappy name for an attribute
that's part of the API, so it's *exactly* the same case as if you had
chosen a crappy name for a public method in Java. Think of public "data"
attributes as magical getter/setters with the most straightforward
behaviour, and of properties as the way to override this default behaviour.

> Plus it's
> really hard to choose crappy accessor name. 

What about getMyCrappyAttributeName/setMyCrappyAttributeName ?-)



-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list