The Pythonic Way of Thinking
by Mike Levin SEO & Datamaster, 05/21/2012
This is to get a second article going about Python, intended to eliminate the need to look up Python examples. I am essentially training myself to “think in Python”, and hope to grow this into a larger article like my first one about the advantages of Python, but for now, I will just start it and let it grow over time.
Okay, in most every programming language I’ve used, I’ve been bound by examples. I never really learned to “think in” a programming language—and really, thank goodness, for where would I be thinking in VBScript? But now the time has come for me to do precisely that with both Python and my text editor vim. I think I nailed a timeless approach… or at least one that will be a competitive advantage for maybe 20 years, and worth transmitting to my daughter.
You should be able to just “think in Python”. It is one of the most ideal languages to fit in your head and make fairly complex tasks just come natural as happens with talking, driving, or any of the other complex tasks humans are able to relegate to automatic response—freeing conscious cycles for more important tasks.
In theory, you should never have to refer to documentation or copy and paste from examples with Python. In practice, this is only partially true, and the top-of-search-results documentation for Python (the official docs) are just awful for new users. This, in my opinion, is one of the key problems with Python. There would be much greater uptake of the language if the documentation were as friendly and accessible as the language itself.
Various key insights need to be made about Python before you don’t need examples and can code things well, and in the Pythonic way. They should be taught first and constantly reinforced. The quintessential example is how any variable or object also has a True or False state in addition to its normal value. In practice, this means the most common value-testing, if x == ‘’, is reduced to just: if x. Any variable whose value is set to zero, variable containing empty (x = ‘’) or empty object (alist = ) equates to False. And there was much rejoicing.
if x != ‘’: print ‘I am True’
if x: print ‘I am True’
Next, you should get a feel for the overarching philosophy of Python by opening a Terminal window, launching the interactive interpreter (just type python), and type “import this”. The philosophic tenants of Python will be echoed back—all this stuff about simple and explicit being best. It’s all a bit abstract, and the polar opposite to the online docs. What we need is something in between.