Should You Learn Python 2 or Python 3?

Should You Learn Python 2 or Python 3?

Image source: from Instagram

Before the advent of Python 3, Python was, in my eyes, a solid and reliable language, with a rich history, simple syntax, powerful features, and a wealth of libraries that left programmers from other languages in awe, exclaiming: “Life is short, I must use Python…” This situation persisted until the emergence of Python 3.

Upgrading a language version is a very common occurrence; not upgrading would be unusual. However, Python 3 is an exception because it abandoned backward compatibility. This means that code written in the Python 2 series, which has been running smoothly, is likely to fail in a Python 3 environment with a 99% chance. For example, if you print a statement in Python 2 like print "Hello World", in Python 3, the system will tell you “SyntaxError: Missing parentheses in call to ‘print'”. Isn’t that shocking?

Why did the developers of Python choose to do this? Or rather, why did Python 3 come into existence? For a long time, no one explained this, as the creators of Python thought it was self-evident. It’s like a person with a great sense of humor laughing uncontrollably at a bad joke, while another person—like an ordinary developer—chews on a cold peanut and asks, “Dude, what are you laughing at?”

Why does Python 3 exist? Brett Cannon—a core developer of Python—finally provided a reasonable explanation during a Q&A session. He had previously assumed that everyone understood why Python 3 was created. He said:

Looking back, I was really naive to think that most people—whether they were new to Python or had been using it for a while—either should know or would be curious enough to seek an explanation or answer. But they didn’t. So I will explain why Python 3 exists. Why break compatibility, change unicode/str/bytes, and make it very difficult to port old code to Python 3.

The fundamental reason is that in Python 2, text and binary data are a mess. For example, the expression ‘abcd’ in Python 3 has a very clear meaning: it is a string containing four letters. However, in Python 2, it can represent either a string of four letters or an array of the ASCII values 97, 98, 99, and 100. You can use print ord('a') to get the ASCII value of that letter. In summary, in Python 2, the meaning of str has two interpretations, which undermines the uniqueness of the language. In Python 3, the answer is unique.

As stated in the “Zen of Python”, “There should be one—and preferably only one—obvious way to do it.” Having text represent both text data and binary data is problematic. Once an object is out of our control, it can cause panic. Some say we can use Unicode, but in practice, people often do not, which can lead to unnecessary troubles. For example, in Python 2, displaying Chinese characters looks like this:

>>> geektime = “极客时间”>>> geektime’xe6e9a2a2′

Python 3 simplifies this significantly:

>>> geektime = “极客时间”>>> geektime’极客时间’

Simplifying the language and removing the ambiguity of str can reduce the error rate in code. Avoiding bugs is very important, yet it is often overlooked. Another saying from the Zen of Python, “Readability counts,” also conveys this idea; ambiguity and implicit knowledge make code difficult to communicate and prone to bugs.

As Brett Cannon mentioned, people sometimes forget how long Python has been around.

In the winter of 1989, Guido began writing the compiler and interpreter for the Python language to have a meaningful Christmas. In February 1991, the first version of Python was born, with the compiler implemented in C. This means that Python appeared before the first version of the Unicode standard was released in October 1991. Later languages, such as Java and Ruby, chose to implement their str types based on Unicode standards, which made Python 3’s situation awkward. In 2004, the proposal for Python 3 began to take shape, as developers realized that supporting Unicode and text from any language was very important.

Python is a language for the world, not just for languages that support ASCII characters. This is the reason why Python 3 chose to use Unicode for text handling.

The development process of Python 3 was roughly as follows:

In 2004, we began designing Python 3. We knew that Python’s popularity was on the rise, and we wanted to maintain that momentum. But this also meant that if we wanted to fix all design flaws in a timely manner to ensure its popularity, it was better to do it now rather than later. We envisioned that Python 3 would last longer than Python 2, and that Python 2.7 would only be used to maintain legacy projects, not for new projects. Therefore, the code written in Python 3 would certainly exceed that written in Python 2. So we decided to endure the pain of transitioning from Python 2 to 3. And under this assumption, we developed Python 3.

We will never do this backward-incompatible thing again.

However, things were clearly not as simple as the developers imagined. Due to the lack of backward compatibility, and the long history of Python 2 with its rich libraries and the fact that most programs are in production environments, the migration cost was too high, and the benefits were not obvious. Additionally, Python 2 was so user-friendly that developers had mastered skills to avoid the pitfalls of str. After releasing Python 3, the core developers of Python believed that the community would eventually abandon the previous version and smoothly transition to the new world. However, contrary to expectations, the coexistence of the two versions lasted nearly a decade. The developers spent more time designing a compatibility subset for Python 2/3 to facilitate this transition.

For the main differences between Python 2 and 3, you can refer to this article:

http://www.runoob.com/python/python-2x-3x.html

So should you learn Python 2 or Python 3? If it were five years ago, I would have recommended learning Python 2. Two years ago, I would have recommended learning both. In fact, in a Mac environment, using homebrew to install and use both versions is very convenient. As of today, a large number of libraries have begun to support Python 3 widely, and the features of Python 3 are no longer just about solving str issues. Therefore, I now recommend that you learn Python 3 directly.

Take Instagram as an example. For a long time, Instagram ran on the combination of Python 2.7 + Django 1.3. After a series of discussions, they ultimately made a significant decision: to upgrade to Python 3.

What advantages does Python 3 have? New features, such as Type Annotations; better performance; and the community’s support focus has completely shifted to Python 3. What reason is there to continue using Python 2?

For the story of Instagram and Python, you can download 极客时间, find the topic in the hotspots section titled Programming Languages, where there is a long article with detailed descriptions.

Should You Learn Python 2 or Python 3?

Related Python Articles:

Life is Short, I Use Python; The Zen of Python; The Rise of Python

Should You Learn Python 2 or Python 3?

Click Read the Original to download the Geek Time iOS version.

Leave a Comment