Accessors in Python (getters and setters)
Bruno Desthuilliers
onurb at xiludom.gro
Thu Jul 13 05:03:40 EDT 2006
Gerhard Fiedler wrote:
> On 2006-07-12 06:17:27, mystilleef wrote:
>
>
>>But developers tend to pay more attention to given methods/functions
>>less crappy names, at least when compared to data attributes. This
>>stems from the fact that in many languages data attributes aren't
>>usually part of the API, as well as the whole OO(Encapsulation) blah
>>blah.
>
>
> I'm not sure about which languages you are talking (pretty much all that
> allow public methods also allow public attributes), but in general I think
> you should get away from the distinction attribute vs method (which doesn't
> make much sense in any language)
And even less when one have first-class functions and computed
attributes... In Python, a method is nothing more than a callable attribute.
> and start looking at the distinction
> public vs private
Since "public" and "private" are now strongly connoted with
"language-inforced access restriction", I'd rather talk of "interface vs
implementation"
> (which is what you really are talking about) -- and start
> giving the appropriate care to naming public entities, no matter /what/
> they are. (Besides, I don't know many coding rules that say that creating
> an accessor get/setTemporaryBuffer that acts on the private member tmp is
> good style.)
>
> I'm just starting to get started with Python, but it seems that the leading
> underscore marks private entities.
Yes, it means "implementation, don't touch or you're on your own".
> So why don't you precede everything with
> an underscore that doesn't have a name that fulfills your criteria for a
> decent public name -- no matter what kind of entity it is?
>
> It seems you are basically complaining that you used a crappy name in a
> public API.
Obviously... And blaming the language for this won't help.
> Well... you shouldn't, not in Python, and not in any other
> language :) And there's no way around it, not in Python, and not in any
> other language, than to rename that entity in the public API. Which can be
> a major hassle, close to impossible. There are all kinds of public APIs
> with crappy names (using accessors and all :) that got created early on and
> never changed. Stuff happens.
>
> Maybe you didn't know about the underscore way to mark private entities.
> Maybe this doesn't work as I think it does
If you think that single leading underscores have a special meaning for
the compiler/interpreter, then you got it wrong. It's a convention. Like
using ALL_CAPS for pseudo-constants. Nothing prevents you from accessing
implementation or modifying "constants" - but then you're on your own
and must not complain when something breaks.
--
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