“My favourite language for maintainability is Python. It has simple, clean syntax, object encapsulation, good library support, and optional named parameters.”
Bram Cohen
The company I’m working for, started a project as a start-up last year. At the beginning there was a discussion on what programming language to choose, so that we could easily have a MVP (Minimum Valuable Product) in the shortest time possible, but also to learn something new while working on this project. The most common programming language used in the company is Java, thus we had to answer a question: do we want to continue with Java or let’s try something else? We chose the 2nd option. But what exactly does this “something else” mean?
First of all, the project is a web application, therefore we had to look first for a web framework and after that for a language that would be compatible with that framework. It didn’t take us too much time to find out the best option, so we opted for Django web framework. As you might know Django is a web framework written in Python, so the choice of the programming language was obvious – it’s Python. That’s how my journey with Python started.
I’ve been developing software using Python for almost one year. When I first started to write code in Python, I had a feeling that I did the wrong choice, but very soon I understood how powerful it is and how easy is to have something working in a short time.
In this article I would like to share with you why I find Python an interesting programming language and how easy it is to work with it, compared to Java or any other language.
Basic example
Let’s start with something trivial – Hello world.
Java:
[code language=”java”]
class Hello {
public static void main( String args[] ) {
System.out.println(" Hello World ");
}
}
[/code]
Python:
[code language=”python”]
print("Hello World")
[/code]
So, how do you like Python, easy peasy right? You don’t need a class, you don’t need a method, do exactly what you need to do, print the text without any additional work. There is not that much syntax in this example, but you could already say that Python syntax is relatively simple, and ease of making a highly readable program that “does the right thing” is a huge advantage.
Indentation
Next thing I like about Python, is that it compels you to correctly indent every line of the code, this being part of syntax checking. This way it tries to make it only one way possible to do anything and makes it a very readable language. Here is an example:
Variables and methods
Another thing I was surprised about, is that in Python you don’t and can’t specify a type for a variable. This makes Python a strong and dynamic typed language. It is strong because the type of the variable doesn’t suddenly change, and dynamic because the program has the responsibility to use built-in functions like isinstance() and issubclass() to test variable types and correct usage at runtime. Someone may say that this way it’s easy to introduce bugs into your programs, but I find it challenging, Try to write correct code. Here is an example:
As you can see in the image above, the variables don’t have any type, so they can be anything – integer, string, list, object etc. In Python, the variable itself if not bound to a type, however the value is. It won’t ever happen that a string could be added to an integer, like in JavaScript.
It is very easy to change the value of the variable, for instance if it has a string assigned to it, but later you want to change it to an integer, just do it. In a statically typed language, the variable has a type, so if you have a variable that’s an integer, you won’t be able to assign any other type of value to it later.
Most probably you have noticed that the method also doesn’t have a return type, again it can be anything. However, it is possible to bind the returned value to a type, but if you do so, you cannot return anything else except something that belongs to that specific type:
Method overloading
Though I needed to overload a method only once, I was a bit disappointed when I had found out that I won’t be able to do so, because Python doesn’t support it, at least there is no standard way of overloading a method, like I’m used to do it in Java.
Even if it is not supported, it is possible, no one stops you from writing methods with the same name, it is valid, but each time you write another function with the same name, the Python interpreter completely forgets about the prior functions with that name. Doesn’t really help, right?
But this is not the end. With some perseverance, you could get the desired result using the multimethods pattern or even better, use default argument values. I find the second option a greater choice, because this creates a function that can be called with fewer arguments than it is defined to allow:
This is just a very small part of what I’ve learned while working with Python. Every time I need to build a specific functionality of the system, I’m astonished by the simplicity and easiness Python provides. To be honest, I’m very excited and I look forward to discovering new capabilities of this language, so stay tuned and don’t miss the next article.