mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
commit
9375ad45af
@ -1,87 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (caiichenr)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Why your Python code should be flat and sparse)
|
||||
[#]: via: (https://opensource.com/article/19/12/zen-python-flat-sparse)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
||||
|
||||
Why your Python code should be flat and sparse
|
||||
======
|
||||
This is part of a special series about the Zen of Python focusing on the
|
||||
fifth and sixth principles: flatness and sparseness.
|
||||
![Digital creative of a browser on the internet][1]
|
||||
|
||||
The [Zen of Python][2] is called that for a reason. It was never supposed to provide easy-to-follow guidelines for programming. The rules are specified tersely and are designed to engage the reader in deep thought.
|
||||
|
||||
In order to properly appreciate the Zen of Python, you must read it and then meditate upon the meanings. If the Zen was designed to be a set of clear rules, it would be a fault that it has rules that contradict each other. However, as a tool to help you meditate on the best solution, contradictions are powerful.
|
||||
|
||||
### Flat is better than nested.
|
||||
|
||||
Nowhere is the pressure to be "flat" more obvious than in Python's strong insistence on indentation. Other languages will often introduce an implementation that "cheats" on the nested structure by reducing indentation requirements. To appreciate this point, let's take a look at JavaScript.
|
||||
|
||||
JavaScript is natively async, which means that programmers write code in JavaScript using a lot of callbacks.
|
||||
|
||||
|
||||
```
|
||||
a(function(resultsFromA) {
|
||||
b(resultsFromA, function(resultsfromB) {
|
||||
c(resultsFromC, function(resultsFromC) {
|
||||
console.log(resultsFromC)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Ignoring the code, observe the pattern and the way indentation leads to a right-most point. This distinctive "arrow" shape is tough on the eye to quickly walk through the code, so it's seen as undesirable and even nicknamed "callback hell." However, in JavaScript, it is possible to "cheat" and not have indentation reflect nesting.
|
||||
|
||||
|
||||
```
|
||||
a(function(resultsFromA) {
|
||||
b(resultsFromA,
|
||||
function(resultsfromB) {
|
||||
c(resultsFromC,
|
||||
function(resultsFromC) {
|
||||
console.log(resultsFromC)
|
||||
}}}
|
||||
```
|
||||
|
||||
Python affords no such options to cheat: every nesting level in the program must be reflected in the indentation level. So deep nesting in Python _looks_ deeply nested. That makes "callback hell" was a worse problem in Python than in JavaScript: nesting callbacks mean indenting with no options to "cheat" with braces.
|
||||
|
||||
This challenge, in combination with the Zen principle, has led to an elegant solution by a library I worked on. In the [Twisted][3] framework, we came up with the _deferred_ abstraction, which would later inspire the popular JavaScript _promise_ abstraction. In this way, Python's unwavering commitment to clear code forces Python developers to discover new, powerful abstractions.
|
||||
|
||||
|
||||
```
|
||||
future_value = future_result()
|
||||
future_value.addCallback(a)
|
||||
future_value.addCallback(b)
|
||||
future_value.addCallback(c)
|
||||
```
|
||||
|
||||
(This might look familiar to modern JavaScript programmers: Promises were heavily influenced by Twisted's deferreds.)
|
||||
|
||||
### Sparse is better than dense.
|
||||
|
||||
The easiest way to make something less dense is to introduce nesting. This habit is why the principle of sparseness follows the previous one: after we have reduced nesting as much as possible, we are often left with _dense_ code or data structures. Density, in this sense, is jamming too much information into a small amount of code, making it difficult to decipher when something goes wrong.
|
||||
|
||||
Reducing that denseness requires creative thinking, and there are no simple solutions. The Zen of Python does not offer simple solutions. All it offers are ways to find what can be improved in the code, without always giving guidance for "how."
|
||||
|
||||
Take a walk. Take a shower. Smell the flowers. Sit in a lotus position and think hard, until finally, inspiration strikes. When you are finally enlightened, it is time to write the code.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/12/zen-python-flat-sparse
|
||||
|
||||
作者:[Moshe Zadka][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/moshez
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/browser_web_internet_website.png?itok=g5B_Bw62 (Digital creative of a browser on the internet)
|
||||
[2]: https://www.python.org/dev/peps/pep-0020/
|
||||
[3]: https://twistedmatrix.com/trac/
|
@ -0,0 +1,86 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (caiichenr)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Why your Python code should be flat and sparse)
|
||||
[#]: via: (https://opensource.com/article/19/12/zen-python-flat-sparse)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
||||
|
||||
为何你的 Python 代码应是扁平与稀疏的
|
||||
======
|
||||
本文是 Python 之禅特别系列当中的一篇,此篇着眼于其中第五与第六条原则:扁平与稀疏。
|
||||
![Digital creative of a browser on the internet][1]
|
||||
|
||||
[Python之禅][2] 得名如此,正是由于它那简明扼要的规则被设计出的意图在于让读者进行深入地思考,而绝不单是为编程提供一份易于遵守的指南。
|
||||
|
||||
读后不去三思其意,断然难以体会 Python 之禅的妙处。倘若 Python 之禅仅仅罗列出一组清晰的法则,那法则之间的矛盾只能算作一种错误。然而用作引导读者去为最优方案沉思的工具,矛盾却是绝佳的。
|
||||
|
||||
### 扁平胜过嵌套 Flat is better than nested.
|
||||
|
||||
迫于对缩进的强硬要求,Python 对“扁平化”的需求显然远超它者。其余编程语言为了缓解对缩进的需求,通常会在嵌套结构里加入一种“欺诈”的手段。用以说明此点,不妨一同看看JavaScript。
|
||||
|
||||
JavaScript 是原生异步的,这意味着程序员用 JavaScript 写的代码会用到大量的回调函数。
|
||||
|
||||
|
||||
```
|
||||
a(function(resultsFromA) {
|
||||
b(resultsFromA, function(resultsfromB) {
|
||||
c(resultsFromC, function(resultsFromC) {
|
||||
console.log(resultsFromC)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
忽略这段代码的具体内容,只去观察这段代码的形状与缩进带来极右点的方式。这种独特的“箭头”图形在我们扫看代码时格外扎眼,这种写法也因此被视作不可取,甚至得到了“回调地狱”的绰号。不过,在 JavaScript 中,这种反映嵌套关系的缩进可以通过“欺诈”来回避。
|
||||
|
||||
|
||||
```
|
||||
a(function(resultsFromA) {
|
||||
b(resultsFromA,
|
||||
function(resultsfromB) {
|
||||
c(resultsFromC,
|
||||
function(resultsFromC) {
|
||||
console.log(resultsFromC)
|
||||
}}}
|
||||
```
|
||||
|
||||
Python 并没有提供这种欺诈手段:每一级嵌套在代码中都如实的对应着一层缩进。因此,Python 深层的嵌套关系在_视觉_上也一定是深层嵌套的。这使得“回调地狱”的问题对于 Python 而言要比在 JavaScript 中严重得多:嵌套的回调函数必定带来缩进,而绝无使用花括号来“欺诈”的可能。
|
||||
|
||||
这项挑战与 Python 之禅的指导原则相结合后,在我参与的库中催生出了一个优雅的解决方案。我们在 [Twisted][3] 框架里提出了 _deferred_ 抽象,日后 JavaScript 中流行的 _promise_ 抽象亦是受其启发而生。正是由于 Python 对整洁代码的坚守,方能推动 Python 开发者去发掘新的,强力的抽象。
|
||||
|
||||
|
||||
```
|
||||
future_value = future_result()
|
||||
future_value.addCallback(a)
|
||||
future_value.addCallback(b)
|
||||
future_value.addCallback(c)
|
||||
```
|
||||
|
||||
(现代 JavaScript 程序员也许会觉得这段代码十分眼熟:Promises 着实受到了 Twisted 里 deferreds 抽象的深远影响。)
|
||||
|
||||
### 稀疏胜过密集 Sparse is better than dense.
|
||||
|
||||
最易降低代码密集程度的方法是引入嵌套。这种习惯也正是有关稀疏的原则要随着前一条提出的原因:在竭尽所能地降低代码密集性之后,我们往往会遗留下_密集_的代码或数据结构。此处的密集,是指塞进过量信息的小段代码,它们会导致错误发生后的解析变得困难。
|
||||
|
||||
这种密集性唯有通过创造性的思考方可改善,此外别无捷径。Python 之禅并不为我们提供简单的解决方案,它只会指明改善代码的方向,而非提供“如何”去做的向导。
|
||||
|
||||
起身走走,泡个热水澡,抑或是闻闻花香。盘坐冥思,直至灵感袭来。当你终于得到启发,便是动身写代码之时。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/12/zen-python-flat-sparse
|
||||
|
||||
作者:[Moshe Zadka][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[caiichenr](https://github.com/caiichenr)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/moshez
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/browser_web_internet_website.png?itok=g5B_Bw62 (Digital creative of a browser on the internet)
|
||||
[2]: https://www.python.org/dev/peps/pep-0020/
|
||||
[3]: https://twistedmatrix.com/trac/
|
Loading…
Reference in New Issue
Block a user