Python and the Principle of Least Astonishment
In this tutorial post, we will learn about what actually Principle of Least Astonishment (POLA) is in Python. We would understand its general meaning and how we use this concept in Python. I will be giving you some cool examples to get a hold on the topic.
Principle of Least Astonishment in Python
We daily come across so many things that we do not even notice and pay attention to. When we do something for a long time we develop some kind of sense. It shows us there is some difference that has suddenly occurred and hints that it appears to be out of the actual pattern. We tie our shoes, open taps in an anti-clockwise direction without even thinking of why we do it that way. We recognize the well-known people just by their voice and do many things in the same way. This is because we have learned it, adapted to it, and start taking the things the way they should. We do not get astonished by these things.
The same happens when we do programming. We learn one language and then another. When we learn one language, we expect other languages also to work the same way. Our brain loves it the same way. But when we find that there is some difference, it makes it difficult to accept it. Then we learn that new style. This concept basically is Principal of Least Astonishment. This is not only a programming principle but also a principle of life.
PRINCIPLE OF LEAST ASTONISHMENT IN PYTHON:
Python developers have understood this concept. On this concept, they say that something which is in the common pattern is pythonic and which is peculiar is unpythonic. Python may seem easy and simple. However, there are many concepts in it that are completely new to the programmers who are not too much into coding. Python is a high-level language that makes use of many predefined functions and libraries. You will sometimes not understand what is going on and will get frustrated. Sometimes Python would not behave the way you want. But we must understand, it is not that Python is wrong, it is because your experience wanted things to happen that way. But Python is working in its own unique manner.
For example call by the reference method. In this method, we deal with the address. But we face some issues while working with Python. So what actually the problem is. This is where Python plays an important role. It works differently. Python makes you write clever codes, use your brain and expand your knowledge.
Let us see an example in Python for Swapping of two numbers to get a better understanding of what I mean.
>>> c = 1 >>> d = 2 >>> c, d = d, c
>>> c, d (2, 1)
In conclusion. we see that Python seems simple and easy because of its very small and crisp codes. This is something that attracts the programmers the most towards itself. If we have our basics strong we can easily go on to learn the higher version of the language. After all, Python has amazing features which makes it one of the most extensively used languages. As mentioned above, it has some differences but this is because of Python’s built-in high-level data types and its dynamic programming features. With practice, we can easily understand these things.