Dusty Phillips: The importance of “One Obvious Way”

9 Sep

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.

— Tim Peters, The Zen of Python

>>> import this

While Python has numerous advantages over other languages that make me reach for it first when starting a new project, I feel the two most important are its emphasis on readability and the community belief in Tim’s words above, “there should be one– and preferably only one –obvious way to do it.” While it’s rare to stumble upon a Python coder that doesn’t implicitly “get” this principle, I find that is frequently misinterpreted when I try to explain Python’s charm to outsiders.

This may be because I often misquote the principle as “the one best way” principle. Many languages have a “one best way” principle. Java, for example, insists that the best way to solve any problem is object oriented programming. Similarly, prolog insists that logic programming is the best way, and Haskell programmers suggests that functional is always the way to go.

Python is not like this at all. Python is pragmatically multi-paradigm. Python has a long history of stealing great ideas from other languages (and, given Guido’s genius, making them better). Python does the very opposite of insisting that there is a best way to do everything.

Orthogonally, I have often had my defense of this principle misinterpreted as having said, “Python is always the best solution to any problem.” This makes me sound arrogant and misinformed, and is patently false. I am keenly aware of Python’s shortcomings. My day job has me pushing Python’s limits on a regular basis, and often, it fails me.

No, Python is not always the best way. The “one obvious way” principle refers to something quite different. It’s not about the language or its libraries at all, it’s more about the programmer’s mindset. Given any specific problem, there are invariably multiple ways to solve it. A Python developer will start out by assuming that one of those assorted solutions is the best solution. The goal of both the Python language and the libraries developed by the community is to make that best solution the obvious one to implement.

This is a form of consistency. Just as pep-8 strongly encourages us to be consistent with all other Python developers in our coding style, the “one obvious way” principle encourages us to be consistent in our choices of algorithms, data structures, and design patterns. In an ideal world, this means that any two Python programmers, given the same problem, would write virtually the same solution. Further, the solution that they proposed would be the best possible solution for the given problem, and all other pythonistas would find it elegantly readable.

This never happens. However, I believe that Python programs are closer to this ideal than many (if not most) other languages. This cohesion means that Python code is often more maintainable, not just by the original developer, but by all members of the Python community.

Some might suggest that strict adherence to the “one obvious way” principle is narrow-minded and restricts experimentation. I heartily disagree. There is clearly not one best web framework in Python. There are numerous web engines available, some more suitable to specific purposes than other. And more are being written every day. I think this is because nobody strictly believes that the most obvious implementation has been found, yet. The poem says there should be one obvious way. It doesn’t say that there always is one. The Python community as a whole is working together to find these best solutions and make them into the obvious solution. A few problems are solved. If you need an iterator, Python’s got you covered. Others are still up in the air, as evidenced by the collection of web frameworks or packaging tools or xml parsers on pypi. Some have only been recently solved; the Requests library is a terrific example. And still more are completely unsolved. The most obvious way I’ve found to handle concurrency and parallelism in Python is: Use Golang. Kivy is a terrific library, but the obvious way to do Android development is still (currently) Java.

In summary, while the ideal of “obviousness” is far, far away, I believe that it is a worthy goal to pursue. I think the mindset attracts a certain kind of programmer, and that those programmers are all as delighted when they discover Python as I was almost a decade ago. I don’t think Python is the best tool for all jobs, nor do I think it is the most enjoyable tool for all programmers. However, for those coders who like a certain kind of obvious, readable, elegance, Python is the language of choice.

via Planet Python http://archlinux.me/dusty/2013/09/09/the-importance-of-one-obvious-way/

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 76 other followers

%d bloggers like this: