In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Why Python code should be flat and sparse". In daily operation, I believe many people have doubts about why Python code should be flat and sparse. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "Why Python code should be flat and sparse"! Next, please follow the editor to study!
Flatter than nested Flat is better than nested
Due to the strong demand for indentation, it is clear that the demand for "flattening" of Python is far higher than that of others. In order to alleviate the need for indentation, other programming languages usually add a means of "cheating" to nested structures. To understand this, take a look at JavaScript.
JavaScript is asynchronous in nature, which means that programmers' code written in JavaScript uses a large number of callback functions.
A (function (resultsFromA) {b (resultsFromA, function (resultsfromB) {c (resultsFromC, function (resultsFromC) {console.log (resultsFromC)})
Ignore the details of this code and just look at the shape of the code and the way it is indented to bring a rightmost point. This unique "arrowhead" figure is particularly eye-catching when we scan the code, so this way of writing is regarded as undesirable, and even got the nickname "callback hell". In JavaScript, however, this indentation reflecting nested relationships can be avoided by "cheating".
A (function (resultsFromA) {b (resultsFromA, function (resultsfromB) {c (resultsFromC, function (resultsFromC) {console.log (resultsFromC)})
Python does not provide this kind of cheating: each level of nesting corresponds truthfully to a layer of indentation in the code. Therefore, the deep nesting relationship of Python must also be visually deeply nested. This makes the problem of "callback hell" much more serious for Python than in JavaScript: nested callback functions must bring indentation and there is no possibility of using curly braces to "cheat".
This challenge, combined with the guiding principles of Python Zen, spawned an elegant solution in the library I participated in. We put forward the deferred abstraction in the Twisted framework, and the popular promise abstraction in JavaScript is also inspired by it. It is Python's commitment to clean code that drives Python developers to explore new and powerful abstractions.
Future_value = future_result () future_value.addCallback (a) future_value.addCallback (b) future_value.addCallback (c)
Modern JavaScript programmers may find this code familiar: promise is really profoundly influenced by the deferred abstraction in Twisted. )
Sparse is better than dense Sparse is better than dense
The easiest way to reduce code intensity is to introduce nesting. This habit is exactly why the principle of sparsity follows the previous one: after doing everything we can to reduce nesting, we tend to leave behind dense code or data structures. Dense here refers to small pieces of code stuffed with too much information, which can make parsing difficult after an error occurs.
This intensity can only be improved through creative thinking, and there are no shortcuts. The Zen of Python does not provide us with a simple solution, it only points the way to improve the code, rather than providing a guide to how to do it.
Get up for a walk, take a hot bath, or smell the flowers. Sit and meditate until inspiration strikes. When you are finally inspired, it is time to start writing code.
At this point, the study on "why Python code should be flat and sparse" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.