Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the importance of Python code consistency

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "what is the importance of Python code consistency". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the importance of Python code consistency.

In the face of ambiguity, In the face of ambiguity refuse the temptation to guess should reject the temptation of guessing.

What should be the result of 1 + "1"? "11" and 2 are guesses. This expression is ambiguous: it will surprise some people no matter what.

Some language choices guess. In JavaScript, the result is "11". In Perl, the result is 2. In C, the result is naturally an empty string. In the face of ambiguity, JavaScript, Perl and C are all guessing.

In Python, this raises TypeError: this is not an error that can be ignored. Capturing TypeError is atypical: it usually terminates the program or at least the current task (for example, in most Web frameworks, it terminates the processing of the current request).

Python declined to guess the meaning of 1 + "1". Programmers must write code with explicit intent: 1 + int ("1"), that is, 2; or str (1) + "1", that is, "11"; or "1" [1:], which will be an empty string. By rejecting guesses, Python makes the program more predictable.

Try to find one, preferably the only obvious solution, There should be one-and preferably only one-obvious way to do it

Predictions can also be biased. Given a task, can you predict the code that will implement the task? Of course, it is impossible to predict perfectly. After all, programming is a creative task.

However, there is no need to intentionally provide multiple ways of redundancy to achieve the same goal. In a sense, some solutions may be "better" or "more Python Pythonic".

The aesthetic appreciation of Python is partly because there can be a healthy debate about which solution is better. You can even continue programming from a different point of view. Even in order to reach agreement, it is possible to accept a dissenting point of view. But under all this, there must be an understanding that the right solution will eventually emerge. We must hope that a real agreement will eventually be reached by agreeing on the best way to achieve the goal.

Although this approach may not be obvious at first (unless you are Dutch) Although that way may not be obvious at first (unless you're Dutch)

This is an important warning: first of all, the best way to achieve a task is often not obvious. Ideas are constantly developing. Python is also evolving. The best way to read files block by block is probably to wait until Python 3.8 to use the walrus operator (: =).

For a common task such as reading files block by block, there is no "only best way" in the nearly 30-year history of Python.

When I started using Python in Python 1.5.2 in 1998, there was no best way to read files line by line. For years, the best way to know if there was a key in a dictionary was to use the keyword .haskey, which didn't change until the in operator appeared.

Just realize that it may take 30 years to find one (and only) way to achieve your goals before Python can keep looking for them. This view of history holds that it is acceptable to take 30 years to do something, but people often feel unused to the United States, a country that has only existed for more than 200 years.

From this part of the Python Zen, the Dutch, whether Guido van Rossum, the creator of Python or the famous computer scientist Edsger W. Dijkstra, have a different worldview. To understand this part, it is essential for Europeans to have a certain sense of time.

At this point, I believe that you have a deeper understanding of the importance of Python code consistency. You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report