Why Python?
A colleague asked me why is Python so popular at Dekart? To my surprise, I failed to provide a clear and concise answer, even though it was me who orchestrated a conscious transition to this language. When the conversation was over I had the feeling I've done a poor job, as I did not produce many supportive arguments.
This story is about what I could have said.
- Mandatory indentation makes the code more readable, expressing structure in a visual way
- Reduced visual noise - there are no curly braces, dollar signs, semicolons or parentheses to distract you, unless necessary
- General purpose - this is a language you can use for almost anything - web services, desktop applications, background daemons, games, scientific research, pretty computer graphics and so on
- A high-level language lets you focus on the logic of the problem you're solving, instead of forcing you to think about low-level implementation details such as managing memory or manipulating strings
- Batteries included - the standard library provides many powerful primitives you can combine to solve a very wide range of problems, without ever having to search for a third party module
- Third party libraries are available, their great variety extends your mileage even further
- Interactive programming - grow your program by writing one line at a time and observing its result immediately, making corrections as needed
- Interactive debugging - pause your program, inspect and manipulate its state before moving on to the next line
- Python is expressive - meaning that it takes a few language constructs (not merely "lines") to express an idea that would take more lines/constructs in another language
- Cross platform compatibility enables you to run your Python code on any of the multitude of supported platforms
- Excellent documentation explains how to use the standard library, it often includes helpful code samples
- Easy start, even for a newbie - you don't need a heavyweight IDE to make a simple `hello world` program, nor do you have to run multiple tools and supply mysterious command line arguments
- Maturity is something you get after a language has been in use for some time and managed to acrete a vibrant, permanent community (unlike toy-languages that come and go)
Some time in ~2004, I was on ICQ with a schoolmate. Our paths have diverged when I joined the Technical University of Moldova, while he studied at a university in the USA. I was curious about his curriculum so I asked what courses they had. Among other things, he mentioned a language I did not know - Python. I liked the name and decided to investigate. I had no idea I was about to embark on a very exciting and empowering journey.
High level language
This means that I can focus on my specific problem, rather than on some low-level elements the solution requires.
We had a computer security course and an assignment to implement an encryption algorithm. Although I had a clear picture of what the steps in the algorithm were, I did not manage to complete the task in the allocated time; I spent most of it writing a "big numbers" library in C.
In gaming terms, I spent two weeks working on a "secondary quest" (multiplying big numbers) instead of working on my "primary quest" (asymmetric encryption).
In another course we had to implement an FTP client. It was the first RFC I have read in my life, I understood the protocol's commands, their order and syntax. However, most of my time was invested in finding out how to create a BSD socket in C and do some basic things with it.
This is akin to playing chess, and thinking about it in terms of "grab this piece with 2 fingers, move it 5cm to the right, grab the piece already there and replace it with the one you're holding", rather than "the rook takes the knight" or "E2->E4".
Is Kasparov good at chess due to his ability to move fingers with extraordinary precision?
***
Fast-forward a couple of months - I switched to writing all of my assignments in Python, unless the conditions explicitly required another language. I got much more productive and life was good! My only regret is that I did not learn Python earlier - so I could leverage it in my other courses.
A funny incident occurred once - there was a practical assignment where we had to use a tool to produce some output and explain how those results correlated with the inputs. I missed the part that said "use this tool" and wrote my own instead, because... it was easy!
Professor [name withheld] didn't believe I wrote the program myself (it was an implementation of Diffie-Hellman key-exchange, something that required a rendezvous with my old friend - big number arithmetic). One of his arguments was "I never heard about this language". Haha!
I have to remind you that this was quite a long time ago, back then there was nobody at the Technical University (to the best of my knowledge) who used this language. There were no courses about it, none of the teachers used or recommended it, neither did any of my colleagues.
Fast-forward a few years, I ended up holding classes at the same university, and guess what I do my demos in? Yep - it is Python, because I can fire up a terminal and grow a program from scratch, in front of my students' eyes, so they can follow me one step at a time and ask questions right away.
In my first years of teaching, it was usually the case that my classes were one's first exposure to Python. I was happy to observe that they're using it in other courses as well.
Nowadays a lot of people already speak this language before my course - that's great. I am sure it makes their academic life more exciting, as they can think about the logic of whatever they're working on, instead of adding * and & haphazardly until "it compiles".
Grow your program
Let me start with an analogy - you're attending a class and the teacher introduces you to a new topic, a slide is shown on the screen - it has a huge and complex diagram on it, many nodes, many links between them.
Compare that with another class, where the teacher starts with a clean slate and writes something on the board. The sketch is extended with another label, another line, each step being accompanied by comments and questions from people in the audience. At the end of the class the board is packed with information.
Which of the lectures was easier to follow and which one left more traces in your memory? It is most likely the latter, because you witnessed its growth from the very beginning.
Programming follows the same pattern. Python makes it easy to adapt a style that is more compatible with my mindset (I suspect it applies to many others too). You can type a line of code and execute it, getting instant feedback and correcting any mistakes you may have made. Immediate feedback is also a prerequisite to entering the state of flow, as researched by Mihaly Czikszentmihalyi (this subject is out of the scope of my story, but I encourage you to look into it). Not all languages provide instant feedback, sometimes you have to change and recompile the code, or run it again through a sequence of steps until it reaches a particular state - then you can try out your new idea.
General purpose
This means that the language does not occupy a narrow niche, instead you can apply it in a wide range of tasks. PHP is a good example - can you do anything other than a web-application in it? You can, but you have to go out on a limb to make that happen, you will be using the wrong tool for the job. The result will be something difficult to maintain and extend.
If you bet everything on PHP, you're locking yourself out of a variety of problems to solve. Python helped me in many courses I had at the university, that is a testimony to its versatility.
Whatever time you invest in sharpening your Python skills is likely to yield benefits in all your interests, not just one or two. This is a basic argument in support of applying Python at work; I told myself that if it made me so productive at the university, the same could happen in another context.
Thus, the reason we heavily rely on Python at Dekart is not because it is fashionable or because someone trendsetter said so, the decision was distilled from my own experience.
What is an expressive language?
A problem can be solved in many ways; a program that solves the problem can be written in different languages. Some of them will require long and convoluted instructions, while others will be more concise. Usually it is better to choose a DSL (domain-specific language), because it provides abstractions tailored for a particular class of problems.
If a DSL is not available, you can leverage an expressive language. What do I mean by that?
An expressive language allows you to get the message across with fewer logical elements.
In the suggested definition I do not rely on "lines of code", but on "logical elements". That is to avoid the trap of having a single line of code that is extremely complex and difficult to debug. A C program can be reformatted to fit in a line, regular expressions (note: regexps are not a programming language) or Perl one-liners can do a lot of useful work in a line too. But...
Tell me what this regexp does:
,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))
What about this C code?
main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t) {return*t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);}
Now that you understand why lines of code are not a good metric, let's move on. Let's say you have to go through each letter in a word and print the order number of that letter in the ASCII table. In abstract terms, we are dealing with three entities - a word, a letter and a number.
In Python I could express that as:
word = "jvompiha" for letter in word: print ord(letter)
In C I would have to do something like this:
char word[] = "jvompiha"; int word_length = strlen(word); for(int i=0; i<word_length-1; i++) { printf("%i", ord(word[i])) }
In the fragment above you have to handle more elements: there's a counter `i`, there's another variable called `word_length`, you have to take care of initializing `i` with a value and incrementing it at each iteration. You must also keep in mind the fact that each variable has a specific type. Somewhere in the back of your mind you must be cautious because "off by one" errors are out there to get you. While you're at it, think about whether `<` should be `<=` instead.
Even though the Python program is shorter, that's not the point, the point is that you only focus on what matters in the context of your problem, whereas in C you have to think about additional meta-details that are not strictly related to the problem at hand.
An analogy would be that of a juggler - is it easier to juggle 2 apples or 5? In these terms, an expressive language does not force you to juggle with more variables than necessary, whereas less expressive languages impose some overhead.
With practice you can juggle even 7 apples or more, but keep in mind that keeping those apples in the air requires a continuous input of energy from your side, and that energy is not free.
The analogy applies not only to the world of physics, but also to the world of abstract thoughts. An interesting experiment in cognitive psychology determined the number of elements one can keep in their working memory, that number is 7 (plus or minus 2).
If a tool allows you to save some time and effort, you can redirect those resources elsewhere, where they matter more.
What pays better?
Every now and then someone asks me which language to learn based on what's popular and what the big companies are looking for. I believe they are asking the wrong question.
You can focus on a language such as ABAP or COBOL, there are employers willing to pay a lot of money for that, due to the fact that there are relatively few people who know these well. Will such skills help you in another context? Very unlikely, and only indirectly.
My guess is that if you're choosing a field of study based on potential profits, programming is not the right choice. You'll be better off as a drug lord, a weapons smuggler or a judge in a corrupt country. These activities will always be more profitable than any programming language, even if you are the only person on the planet who knows it. If you're in it for the money, that is.
Conclusions
Python is a great language, not only for beginners but also for professionals who use programming to solve problems in the real world. If you had to learn only one language, I would be inclined to recommend Python.
However, keep in mind that if you become emotionally attached to a solution, it can become your next problem.
Reality often realigns the planets into configurations that make you apply other languages. Although I am a huge fan of Python (to the extent that I made my own Python themed T-shirt), I use other programming languages on a daily basis. I think that makes me a better person.
There are many languages that provide the benefits discussed above (though Python brings all of them together in one package) and there is value in learning a new one.
Pre-emptive counter counter-arguments
An attentive reader might point out that everything I wrote above can apply to other languages too. C, for example, is a general purpose language, with excellent documentation, it is cross-platform, it pays well, and so on. Not only that, but it is also a high level language (when compared to platform-specific assembly).
Besides that, one can accumulate so much experience in any given language, that they "don't even see the code, they see `blonde`, `brunette`...". The thing to keep in mind is the learning curve - how much time does one have to invest in the study of a language before they can produce some useful results with it?
The bottom line is that you should treat this story as pro-Python, rather than anti-everythingElse.