5 ms·
> using public fields in a non trivial project is a sure receipt for disaster. This is just dogma. Every Python project in existence has 100% public fields. So
by et1337 10y ago
> using public fields in a non trivial project is a sure receipt for disaster.
This is just dogma. Every Python project in existence has 100% public fields. Some are disasters, some are beautiful. Only a Sith deals in absolutes.
- eximius 10y agoBetter than ugly, beautiful is. Better then implicit, explicit is. Better than complex, simple is. Better than complicated, complex is. Better than nested, flat is. Better than dense, sparse is. Counts, readability does. Special enough to break the rules, special cases are not. But beaten by practicality, purity is. Silently passed, an error should never be. Unless explicitly, is it silenced. In the face of ambiguity, the temptation to guess, refuse you must. One, preferably only one, way to do it, there should be. Not obvious, it might be. Better than never, is now. But often better than right now, is never. If hard to explain, bad it is. If easy to explain, good it may be. Namespaces are a honking good idea - more of them we should do! -- The Zen of Python, Yoda
- qwertyuiop924 10y agoNow those are some best practices I can live by.
- balnaphone 10y ago(an attempt to translate to standard english grammar, for the benefit of other non-native English readers, who may also struggle to parse this) Beautiful is better than ugly. Explicit is better then implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases are not special enough to break the rules. But purity is beaten by practicality. An error should never be silently passed. Unless it is silenced explicitly. You must refuse the temptation to guess in the face of ambiguity. There should be one, preferably only one, way to do it. It might not be obvious. Now is better than never. But never is often better than right now. It is bad if it is hard to explain. It may be good if it is easy to explain. Namespaces are a honking good idea - we should do more of them!
- cariaso 10y agoinstead of offering your own translation, we can instead go back to the original english. Run 'python -m this' and you will see The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
- kazinator 10y ago> Namespaces are a honking good idea - we should do more of them! That should be: english.namespaces english.verbs.are english.articles.a american.english.vernacular.adjective.honking ...
- qwertyuiop924 10y agoNo, that's Java. Flat is better than nested: no self-respecting Python programmer would write hierarchy that deep.
- joesb 10y agoSo which rule take precedents here? Namespace is obviously not flat, at least it's more nested than one without namespace. That's the problem I have with people praising Zen of Python as if it means anything. It's like a bible you just pick the verse you like to justify your action even if it might conflict with other rules. Then you praise the whole thing for being so wise.
- qwertyuiop924 10y agoIt means a lot: good python code follows it. However, yes, some of the verses conflict, because it turns out that good advice sometimes contradicts itself. All I can say is don't go too far in either direction.
- LyndsySimon 10y agoMuch of the point of the Zen of Python is that it's self-contradictory.
- eximius 10y agoBalanced, you must be.
- ubernostrum 10y agoNo, Java looks like the famous quotation about the nail and the horse from: http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom...
- tigershark 10y agoIf there are inexperienced programmers working on it, as in my preamble, then almost surely it is in whatever language that doesn't prohibit to mutate objects indiscriminately. In F#, Haskell and other languages that enforce immutability obviously it isn't a problem, in Python most surely it is.
- et1337 10y agoEh. Inexperienced programmers will find a way to screw things up somehow. Probably by over-engineering, like the original article describes.
- kilink 10y agoI think the advice is more relevant in the context of the specific language; it's not universal. In python you can go from attribute access to using a property (getter/setter) without breaking anything. The same is not true in a language like Java where obj.foo is always a direct field access distinct from calling a method like obj.getFoo(), so going from public fields to getters is not backwards compatible and can be painful.
- qwertyuiop924 10y agoHowever, it's still a simple enough change that you shouldn't build getter/setters unless you're already pretty sure it'll be changed. OTOH, if you're using a language with generated getter/setters (ruby, smalltalk, lisp), just do it.
- superbatfish 10y ago>In python you can go from attribute access to using a property (getter/setter) without breaking anything. True, but that should be avoided if possible. Python 'properties' violate the principal of "explicit is better than implicit". Once you realize "Oops, I need an accessor function here", the lazy programmer says "Aww, grepping for all uses of .foo and replacing them with .getFoo() will take 20 minutes. Instead, I'll just redefine it as a property and no one will notice." If you care about quality, go the extra mile: make it clear to the people reading your code that a function is being called. Properties are a kinda nice language feature, but they are so frequently misused that I think the language would have been better off without them. They encourage bad habits.
- sbov 10y agoUsing find & sed is not painful. I use getters and setters, but still think you will waste more time arguing about this issue, than leaving it be and finding out you have to change them down the line. Edit: also, especially for getters, I like my accessors to be simple accessors. Hiding too much code behind them can be unpleasantly surprising, so as they deviate further from accessors I like to rename them - e.g. CalculateXxxx rather than GetXxx. Fewer surprises. Given that, I potentially have an issue with continuing to call it GetXXX or SetXXX in the face of certain changes.