Accessors in Python (getters and setters)

Bruno Desthuilliers onurb at xiludom.gro
Thu Jul 13 06:25:40 EDT 2006


mystilleef wrote:
> Bruno Desthuilliers wrote:
> 
>>mystilleef wrote:
>>
>>>Marc 'BlackJack' Rintsch wrote:
>>>
>>>
>>>>In <1152778446.236509.151730 at 35g2000cwc.googlegroups.com>, mystilleef
>>>>wrote:
>>>>
>>>>
>>>>
>>>>>Maric Michaud wrote:
>>
>>(snip)
>>
>>
>>>>>>But they are in Python and that is the python's philosophy. All attribute or
>>>>>>method not beginning with an '_' *is* API.
>>>>>
>>>>>Right, and what if I want to change a private API to a public one. How
>>>>>does that solve my naming issues.
>>>>
>>>>Then you have to change all references to that private attribute.  What's
>>>>the problem here?  As it was private I would expect to find all the
>>>>references "nearby" in the same module or class.
>>>
>>>Right, but tmp isn't private.
>>
>>Right, but that's your choice. Would you complain about "almost any
>>other language" if you had to hunt a badly named public method thru 27KLOC ?
>>
> 
> Methods are used to perform actions. Data attributes usually aren't. We
> are on different planes.

Please explain the profound *semantic* difference between:

val = obj.getVal()
obj.setVal(val)

and

val = obj.val
pbj.val = val



>>(snip)
>>
>>
>>>>Python is not almost all other languages and in Python code you usually
>>>>won't find those trivial getters and setters because we have properties if
>>>>the access might become a bit more complex in the future.
>>>>
>>>
>>>Ha! I bet you haven't read too many Python codes.
>>>
>>
>>I have read tens of thousands LOC of Python in the seven past years.
>>Computed attributes came in with 2.2x IIRC, so there's a lot of 'legacy'
>>code that uses getters and setters. Porting this code to a
>>computed-attribute style would break all client code. Having the two
>>schemes coexisting would make for bloated APIs and
>>too-many-ways-to-do-it. So we live with this until Py3K. And none of
>>these considerations contradicts the point that there's no more use for
>>javaish getters/setters in Python, nor that javaish getters/setters are
>>not pythonic.
>>
> 
> I never made any contradictory points regarding the matter. I wanted to
> know the pythonic way of using accessors in Python.

Yes, and you had the answer.

> That's all. You are
> the person making wild assumptions about my queries,
> programming background and code.

Which "wild assumptions" ? I'm only replying to things you post here.

Namely, you wrote this:
"
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.
"

As anyone here has tried to explain, this assertion is plain wrong. I
agree that Diez's post (you were responding too) was a bit aggressive,
but that doesn't make your assertion more valid.


-- 
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