Aktuelle Änderungen Printable View Änderungen Bearbeiten
CodeSmells > PythonSelf > OnlyPublicFieldsInPythonClear TrailMy convention, attributes with names prefixed with a single underscore are private. There's nothing to stop anyone using these, but, well, if you take the back off the radio, the warranty is void.[1]
> Do you ever heard of that funny things named "an interface" and "an
> implementation"?
the "shared DLL:s ought to work" school of thought, you mean?[2]
>> The engineering decisions regarding my application should be on my
>> side, not the language lawyers.
>
> No problem, but any decision like that should be expressed in
> "writing", by re-declaring the variable so it's no longer private.
How do you see this working in practice?
Say you have written a class, with a private variable. I decide that I need access to that variable, for reasons you never foresaw. What happens next? I ask you nicely to please change your class and turn that private attribute into a public one. What happens if you refuse? Can I have you taken out and shot and seize ownership of your class, or do I have to copy and paste your class into my code, creating a duplicate class I can modify as much as I like?[3]
if you think that once you've put private labels on all accidental stuff, nothing will break during upgrades, you're clearly very new to this thing called programming... [4]
"Software lives" and totalitarian laws destroy easy living.[5]
Do you know any language that has real private and protected attributes?
There may be some. I don't know. But anyone who says "C++" is fooling themselves:
#define private public
And your "real private and protected" objects are no longer private or protected.[6]
There are two philosophies about programming:
-- Make it hard to do wrong.
-- Make it easy to do right.
What you are promoting is the first philosophy: Tie the programmer's hands so he can't do wrong. Python for the most part follows the second philosophy, making writing good code so easy that the coder is rarely tempted to commit any evil.[7]
BoilerplateI don't want to get into politics, but this IS very much what happened East of the Iron Curtain -- far too much control, indeed so much that the first serious attempt to release it crumbled the whole system to not much above feudal/anarchy. The excesses of control in the West were fortunately moderate enough that they could to some extent be (partially) unwound less explosively. I'm not arguing for anarchy, mind you -- Somalia is a living example of what anarchy means; just that, quite apart from ethical issues, from a strictly engineering, effectiveness standpoint there is a reasonably flat optimal region, well short of totalitarian states but much more controlled than 'everyone for himself'.[8]
> What is the benefit of "public by default"?
Minimizing boilerplate. 90% of exposed properties in languages and object models which don't support that lead to a lot of boilerplate:
...
There is no benefit whatsoever in all that boilerplate. Maybe, if one has been educated to such strict discipline, there is a "feel-good factor" -- "See, Mum, I'm being a good boy and only accessing Pliffo via an accessor method!". But quite apart from program efficiency (that's not a major issue AT ALL), all that extra, useless code is a dead-weight dragging your productivity down -- sure, some silly "Wizard" can originally generate it, but _people_ are going to have to maintain and test and read and document it forevermore. Sheer dead weight. [9]
What about : "encapsulation does not mean *language enforced* restriction" then ?
To me, encapsulation means that - as a client - you do not *need* to care about implementation details, not that you can not get at it. And while data hiding is indeed a possible mean to enforce some kind of encapsulation, it's not quite the same thing.[10]
In Python, the philosophy "we're all consenting adults here" applies.
You shouldn't pretend to know, at the time you write it, all the uses to which your code will be put. Barriers such as enforced "private" attributes will only cause resentment when people, despite your anticipations, *need* to access them and are then forced to hack their way around them.[11]
Perhaps you're confusing the "encapsulation" and "abstraction" principles for enforced access restrictions; they're not.[12]