diff --git a/translated/tech/20190925 Debugging in Emacs- The Grand Unified Debugger.md b/published/20190925 Debugging in Emacs- The Grand Unified Debugger.md similarity index 57% rename from translated/tech/20190925 Debugging in Emacs- The Grand Unified Debugger.md rename to published/20190925 Debugging in Emacs- The Grand Unified Debugger.md index 7bdeff9b48..ef4b71f194 100644 --- a/translated/tech/20190925 Debugging in Emacs- The Grand Unified Debugger.md +++ b/published/20190925 Debugging in Emacs- The Grand Unified Debugger.md @@ -1,35 +1,32 @@ [#]: collector: (lujun9972) [#]: translator: (cycoe) -[#]: reviewer: ( ) -[#]: publisher: ( ) -[#]: url: ( ) +[#]: reviewer: (wxy) +[#]: publisher: (wxy) +[#]: url: (https://linux.cn/article-11994-1.html) [#]: subject: (Debugging in Emacs: The Grand Unified Debugger) [#]: via: (https://opensourceforu.com/2019/09/debugging-in-emacs-the-grand-unified-debugger/) [#]: author: (Vineeth Kartha https://opensourceforu.com/author/vineeth-kartha/) -Emacs 调试秘籍之——GUD 调试器 +Emacs 调试秘籍之 GUD 调试器 ====== -[![][1]][2] +![][2] -_本文简短地对 Emacs 的调试工具 GUD(译注:全称 Grand Unified Debugger,鉴于其缩写形式更为人熟知,以下全文将使用缩写替代此全称)的特性进行了探索。_ +> 本文简短地对 Emacs 的调试工具 GUD 的特性进行了探索。 -如果你是一个 C 或 C++ 开发者,你很可能已经使用过 GDB(GNU 调试器),毫无疑问,它是现今最强大、最无可匹敌的调试器之一。它唯一的缺点就是它基于命令行,虽然仍能提供许多强大的功能,但有时也会具有一些局限性。这也就是为什么聪明的人们开始追求整合了编辑器和调试器的图形化GUI集成Integrated开发Development环境Environment。仍有些开发者坚信使用鼠标会降低工作效率,在 GUI 上用鼠标点~点~点~是来自恶魔的诱惑。 +如果你是一个 C 或 C++ 开发者,你很可能已经使用过 GDB(GNU 调试器),毫无疑问,它是现今最强大、最无可匹敌的调试器之一。它唯一的缺点就是它基于命令行,虽然仍能提供许多强大的功能,但有时也会具有一些局限性。这也就是为什么聪明的人们开始追求整合了编辑器和调试器的图形化 GUI 集成开发环境Integrated Development Environment。仍有些开发者坚信使用鼠标会降低工作效率,在 GUI 上用鼠标点~点~点~是来自恶魔的诱惑。 因为 Emacs 是现今最酷的文本编辑器之一,我将为你展示如何在不碰鼠标且不离开 Emacs 的情况下,实现写代码、编译代码、调试代码的过程。 -![图 1: Emacs 迷你缓冲区中的编译命令][3] +GUD(LCTT 译注:全称大统一调试器Grand Unified Debugger,鉴于其缩写形式更为人熟知,以下全文将使用缩写替代此全称)是 Emacs 下的一个模式mode,用于在 Emacs 中运行 GDB。它向 GDB 提供了 Emacs 的所有特性,使用户无需离开编辑器就可以对代码进行调试。 -![图 2: 编译状态][4] +### 使用 GUD 的前期准备 -GUD 是 Emacs 下的一个模式mode,用于在 Emacs 中运行 GDB。它向 GDB 提供了 Emacs 的所有特性,使用户无需离开编辑器就可以对代码进行调试。 - -**使用 GUD 的前期准备** 如果你正在使用一个 Linux 机器,很可能你已经安装了 GDB 和 gcc,接下来就是要确保已经安装了 Emacs。以下的内容我将假设读者熟悉 GDB 并且至少用它做过基本的调试。如果你未曾接触过 GDB,你可以做个快速入门,这些资料在网上随处可得。 -对于那些 Emacs 新手,我将向你介绍一些基本术语。纵览整篇文章,你将看到诸如 _C-c M-x_ 等快捷键。此处 _C_ 代表 _Ctrl_ 键,_M_ 代表 _Alt_ 键。_C-c_ 代表 _Ctrl_ 键和 _c_ 键被同时按下。如果你看到 _C-c c_,它代表同时按下 _Ctrl_ 键和 _c_ 键,紧接着按下 _c_ 键。在 Emacs 中,编辑文本的主要区域被称为main缓冲区buffer,而在 Emacs 窗口下方用于输入命令的区域被称为迷你mini缓冲区buffer。 +对于那些 Emacs 新手,我将向你介绍一些基本术语。纵览整篇文章,你将看到诸如 `C-c M-x` 等快捷键。此处 `C` 代表 `Ctrl` 键,`M` 代表 `Alt` 键。`C-c` 代表 `Ctrl` 键和 `c` 键被同时按下。如果你看到 `C-c c`,它代表同时按下 `Ctrl` 键和 `c` 键,释放后紧接着按下 `c` 键。在 Emacs 中,编辑文本的主要区域被称为主缓冲区main buffer,而在 Emacs 窗口下方用于输入命令的区域被称为迷你缓冲区mini buffer。 -启动 Emacs,并按下 _C-x C-f_ 来创建一个新文件。Emacs 将提示你输入一个文件名,此处让我们将文件命名为 `buggyFactorial.cpp`。一旦文件打开,输入如下代码: +启动 Emacs,并按下 `C-x C-f` 来创建一个新文件。Emacs 将提示你输入一个文件名,此处让我们将文件命名为 `buggyFactorial.cpp`。一旦文件打开,输入如下代码: ``` #include @@ -48,32 +45,38 @@ int main() { } ``` -使用 _C-x C-s_ 快捷键保存文件。文件保存完毕,是时候进行编译了。按下 _M-x_,在弹出的提示符prompt后输入 _compile_ 并点击回车。然后在提示符后,将内容替换为 _g++ -g buggyFactorial.cpp_ 并再次点击回车。 +使用 `C-x C-s` 快捷键保存文件。文件保存完毕,是时候进行编译了。按下 `M-x`,在弹出的提示符prompt后输入 `compile` 并点击回车。然后在提示符后,将内容替换为 `g++ -g buggyFactorial.cpp` 并再次点击回车。 + +![图 1: Emacs 迷你缓冲区中编译命令][3] 这将在 Emacs 中开启另一个缓冲区,显示编译的状态。如果你的代码输入没有错误,你将预期得到如图 2 所示的缓冲区。 -要想隐藏编译缓冲区,首先确保你的光标在编译缓冲区中(你可以不使用鼠标,而是通过 _C-x o_ 快捷键将光标从一个缓冲区移动到另一个),然后按下 _C-x 0_。下一步就是运行代码,并观察是否运行良好。按下 _M-!_ 快捷键并在迷你缓冲区的提示符后面输入 _./a.out_。 +![图 2: 编译状态][4] -你可以看到迷你缓冲区中显示断言失败。很明显代码中有错误,因为 5 的阶乘是 120。那么让我们现在开始调试吧。 +要想隐藏编译缓冲区,首先确保你的光标在编译缓冲区中(你可以不使用鼠标,而是通过 `C-x o` 快捷键将光标从一个缓冲区移动到另一个),然后按下 `C-x 0`。下一步就是运行代码,并观察是否运行良好。按下 `M-!` 快捷键并在迷你缓冲区的提示符后面输入 `./a.out`。 ![图 3: 代码在迷你缓冲区中的输出][5] +你可以看到迷你缓冲区中显示断言失败。很明显代码中有错误,因为 5 的阶乘是 120。那么让我们现在开始调试吧。 + +### 使用 GUD 调式代码 + +现在,我们的代码已经编译完成,是时候看看到底哪里出错了。按下 `M-x` 快捷键并在提示符后输入 `gdb`。在接下来的提示符后,输入 `gdb -i=mi a.out`。如果一切顺利,GDB 会在 Emacs 缓冲区中启动,你会看到如图 4 所示的窗口。 + ![图 4: Emacs 中的 GDB 缓冲区][6] -**使用 GUD 调式代码** -现在,我们的代码已经编译完成,是时候看看到底哪里出错了。按下 _M-x_ 快捷键并在提示符后输入 _gdb_。在接下来的提示符后,输入 _gdb -i=mi a.out_。如果一切顺利,GDB 会在 Emacs 缓冲区中启动,你会看到如图 4 所示的窗口。 -在 _gdb_ 提示符后,输入 _break main_ 来设置断点,并输入 _r_ 来运行程序。程序会开始运行并停在 _main()_ 函数处。 +在 `gdb` 提示符后,输入 `break main` 来设置断点,并输入 `r` 来运行程序。程序会开始运行并停在 `main()` 函数处。 -一旦 GDB 到达了 _main_ 处设置的断点,就会弹出一个新的缓冲区显示你正在调试的代码。注意左侧的红点,正是你设置断点的位置,同时会有一个小的标志提示你当前代码运行到了哪一行。当前,该标志就在断点处(如图 5)。 +一旦 GDB 到达了 `main` 处设置的断点,就会弹出一个新的缓冲区显示你正在调试的代码。注意左侧的红点,正是你设置断点的位置,同时会有一个小的标志提示你当前代码运行到了哪一行。当前,该标志就在断点处(如图 5)。 ![图 5: GDB 与代码显示在两个分离的窗口][7] -![图 6: 在 Emacs 中使用独立帧显示局部变量][8] - -为了调试 _factorial_ 函数,我们需要单步运行。想要达到此目的,你可以使用 _gdb_ 提示符和 GDB 命令 _step_,或者使用 Emacs 快捷键 _C-c C-s_。还有其它一些快捷键,但我更喜欢 GDB 命令。因此我将在本文的后续部分使用它们。 +为了调试 `factorial` 函数,我们需要单步运行。想要达到此目的,你可以在 GBD 提示符使用 GDB 命令 `step`,或者使用 Emacs 快捷键 `C-c C-s`。还有其它一些快捷键,但我更喜欢 GDB 命令。因此我将在本文的后续部分使用它们。 单步运行时让我们注意一下局部变量中的阶乘值。参考图 6 来设置在 Emacs 帧中显示局部变量值。 +![图 6: 在 Emacs 中使用独立帧显示局部变量][8] + 在 GDB 提示符中进行单步运行并观察局部变量值的变化。在循环的第一次迭代中,我们发现了一个问题。此处乘法的结果应该是 5 而不是 4。 本文到这里也差不多结束了,读者可以自行探索发现 GUD 模式这片新大陆。GDB 中的所有命令都可以在 GUD 模式中运行。我将此代码的修复留给读者作为一个练习。看看你在调试的过程中,可以做哪一些定制化,来使你的工作流更加简单和高效。 @@ -85,17 +88,17 @@ via: https://opensourceforu.com/2019/09/debugging-in-emacs-the-grand-unified-deb 作者:[Vineeth Kartha][a] 选题:[lujun9972][b] 译者:[cycoe](https://github.com/cycoe) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 [a]: https://opensourceforu.com/author/vineeth-kartha/ [b]: https://github.com/lujun9972 -[1]: https://i0.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Screenshot-from-2019-09-25-15-39-46.png?resize=696%2C440&ssl=1 (Screenshot from 2019-09-25 15-39-46) -[2]: https://i0.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Screenshot-from-2019-09-25-15-39-46.png?fit=800%2C506&ssl=1 -[3]: https://i1.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_1.png?resize=350%2C228&ssl=1 -[4]: https://i2.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_2.png?resize=350%2C228&ssl=1 -[5]: https://i0.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_3.png?resize=350%2C228&ssl=1 -[6]: https://i0.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_4.png?resize=350%2C227&ssl=1 -[7]: https://i1.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_5.png?resize=350%2C200&ssl=1 -[8]: https://i1.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_6.png?resize=350%2C286&ssl=1 +[1]: https://i0.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Screenshot-from-2019-09-25-15-39-46.png?ssl=1 +[2]: https://i0.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Screenshot-from-2019-09-25-15-39-46.png?ssl=1 +[3]: https://i1.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_1.png?ssl=1 +[4]: https://i2.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_2.png?ssl=1 +[5]: https://i0.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_3.png?ssl=1 +[6]: https://i0.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_4.png?ssl=1 +[7]: https://i1.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_5.png?ssl=1 +[8]: https://i1.wp.com/opensourceforu.com/wp-content/uploads/2019/09/Figure_6.png?ssl=1 diff --git a/translated/tech/20200311 How to install pip to manage PyPI packages easily.md b/published/20200311 How to install pip to manage PyPI packages easily.md similarity index 56% rename from translated/tech/20200311 How to install pip to manage PyPI packages easily.md rename to published/20200311 How to install pip to manage PyPI packages easily.md index 8c188e1acb..b6453085c1 100644 --- a/translated/tech/20200311 How to install pip to manage PyPI packages easily.md +++ b/published/20200311 How to install pip to manage PyPI packages easily.md @@ -1,58 +1,56 @@ [#]: collector: (lujun9972) [#]: translator: (HankChow) -[#]: reviewer: ( ) -[#]: publisher: ( ) -[#]: url: ( ) +[#]: reviewer: (wxy) +[#]: publisher: (wxy) +[#]: url: (https://linux.cn/article-11993-1.html) [#]: subject: (How to install pip to manage PyPI packages easily) [#]: via: (https://opensource.com/article/20/3/pip-linux-mac-windows) [#]: author: (Vijay Singh Khatri https://opensource.com/users/vijaytechnicalauthor) 安装 pip 轻松管理 PyPI 软件包 ====== + > 在 Linux、Mac 或 Windows 上为旧版 Python 安装 pip。 -![Person typing on a 1980's computer][1] +![](https://img.linux.net.cn/data/attachment/album/202003/14/102539wq3a73opalpzoabb.jpg) -Python 是一种功能强大、流行广泛的编程语言,在常规编程、数据科学等很多方面它都有丰富的软件包可供使用。但这些软件包通常都不会在 Python 安装时自动附带,而是需要由用户自行下载、安装和管理。所有的这些软件包(包括库和框架)都存放在一个名叫 [PyPI][2](也就是 Python 软件包索引Package Index)的中央存储库当中,而 pip(也就是首选安装程序Preferred Installer Program)则是管理这个中央存储库的工具。 +Python 是一种功能强大、流行广泛的编程语言,在常规编程、数据科学等很多方面它都有丰富的软件包可供使用。但这些软件包通常都不会在 Python 安装时自动附带,而是需要由用户自行下载、安装和管理。所有的这些软件包(包括库和框架)都存放在一个名叫 [PyPI][2](也就是 Python 软件包索引 Python Package Index)的中央存储库当中,而 `pip`(也就是首选安装程序Preferred Installer Program)则是管理这个中央存储库的工具。 -在安装 pip 之后,管理 PyPI 的软件包就变得很方便了。大部分的软件包都可以通过在终端或者命令行界面执行 `python -m pip install <软件包名>` 这样的命令来完成安装。 +在安装 `pip` 之后,管理 PyPI 的软件包就变得很方便了。大部分的软件包都可以通过在终端或者命令行界面执行 `python -m pip install <软件包名>` 这样的命令来完成安装。 -较新版本的 Python 3(3.4 或以上)和 Python 2(2.7.9 或以上)都已经预装了 pip,旧版本的 Python 没有自带 pip,但可以另外安装。 +较新版本的 Python 3(3.4 或以上)和 Python 2(2.7.9 或以上)都已经预装了 `pip`,旧版本的 Python 没有自带 `pip`,但可以另外安装。 -在这篇文章中,我将会介绍 pip 在 Linux、Mac 和 Windows 系统上的安装过程。想要了解更多详细信息,可以参考 [pip.pypa][3] 的文档。 +在这篇文章中,我将会介绍 `pip` 在 Linux、Mac 和 Windows 系统上的安装过程。想要了解更多详细信息,可以参考 [pip.pypa][3] 的文档。 ### 首先需要安装 Python -首先你的系统中需要安装好 Python,否则 pip 安装器无法理解任何相关的命令。你可以在命令行界面、Bash 或终端执行 `python` 命令确认系统中是否已经安装 Python,如果系统无法识别 `python` 命令,请先[下载 Python][4] 并安装。安装完成后,你就可以看到一些引导你安装 pip 的提示语了。 +首先你的系统中需要安装好 Python,否则 `pip` 安装器无法理解任何相关的命令。你可以在命令行界面、Bash 或终端执行 `python` 命令确认系统中是否已经安装 Python,如果系统无法识别 `python` 命令,请先[下载 Python][4] 并安装。安装完成后,你就可以看到一些引导你安装 `pip` 的提示语了。 ### 在 Linux 上安装 pip -在不同发行版的 Linux 上,安装 pip 的命令也有所不同。 +在不同发行版的 Linux 上,安装 `pip` 的命令也有所不同。 在 Fedora、RHEL 或 CentOS 上,执行以下命令安装: - ``` -`$ sudo dnf install python3` +$ sudo dnf install python3 ``` -在 Debian 或 Ubuntu 上,使用 apt 包管理工具安装: - +在 Debian 或 Ubuntu 上,使用 `apt` 包管理工具安装: ``` -`$ sudo apt install python3-pip` +$ sudo apt install python3-pip ``` -其它的发行版可能还会有不同的包管理工具,比如 Arch Linux 使用的是 pacman: - +其它的发行版可能还会有不同的包管理工具,比如 Arch Linux 使用的是 `pacman`: ``` -`$ sudo pacman -S python-pip` +$ sudo pacman -S python-pip ``` -执行 `pip --version` 命令就可以确认 pip 是否已经正确安装。 +执行 `pip --version` 命令就可以确认 `pip` 是否已经正确安装。 -如果到这里你已经在 Linux 系统上成功安装 pip,你可以直接跳到“使用 pip”部分继续阅读。 +在 Linux 系统上安装 `pip` 就是这样简单,你可以直接跳到“使用 pip”部分继续阅读。 ### 在 Mac 上安装 pip @@ -60,77 +58,68 @@ MacOS 已经预装了 Python,但 Python 版本一般是比较旧的。如果 在 Mac 上可以使用 [homebrew][7] 安装 Python 3: - ``` -`$ brew update && brew upgrade python` +$ brew update && brew upgrade python ``` -如果你安装的是以上提到的新版本 Python 3,它会自带 pip 工具。你可以使用以下命令验证: - +如果你安装的是以上提到的新版本 Python 3,它会自带 `pip` 工具。你可以使用以下命令验证: ``` -`$ python3 -m pip --version` +$ python3 -m pip --version ``` 如果你使用的是 Mac,安装过程也到此结束,可以从“使用 pip”部分继续阅读。 ### 在 Windows 上安装 pip -以下 pip 安装过程是针对 Windows 8 和 Windows 10 的。下面文章中的截图是 Windows 10 环境下的截图,但对 Windows 8 同样适用。 +以下 `pip` 安装过程是针对 Windows 8 和 Windows 10 的。下面文章中的截图是 Windows 10 环境下的截图,但对 Windows 8 同样适用。 首先确认 [Python 已经安装完成][8]。 如果你想在 Windows 系统上像 Linux 一样使用包管理工具,[Chocolatey][9] 是一个不错的选择,它可以让 Python 的调用和更新变得更加方便。Chocolatey 在 PowerShell 中就可以运行,只需要简单的命令,Python 就可以顺利安装。 - ``` -`PS> choco install python` +PS> choco install python ``` -接下来就可以使用 pip 安装所需的软件包了。 +接下来就可以使用 `pip` 安装所需的软件包了。 ### 使用 pip -在 Linux、BSD、Windows 和 Mac 上,pip 都是以同样的方式使用的。 +在 Linux、BSD、Windows 和 Mac 上,`pip` 都是以同样的方式使用的。 例如安装某个库: - ``` -`python3 -m pip install foo --user` +python3 -m pip install foo --user ``` 卸载某个已安装的库: - ``` -`python3 -m pip uninstall foo` +python3 -m pip uninstall foo ``` 按照名称查找软件包: - ``` -`python3 -m pip search foo` +python3 -m pip search foo ``` -将 pip 更新到一个新版本: - +将 `pip` 更新到一个新版本: ``` -`$ sudo pip install --upgrade pip` +$ sudo pip install --upgrade pip ``` 需要注意的是,在 Windows 系统中不需要在前面加上 `sudo` 命令,这是因为 Windows 通过独有的方式管理用户权限,对应的做法是[创建一个执行策略例外][10]。 - ``` -`python -m pip install --upgrade pip` +python -m pip install --upgrade pip ``` 希望本文介绍的的方法能对你有所帮助,欢迎在评论区分享你的经验。 - -------------------------------------------------------------------------------- via: https://opensource.com/article/20/3/pip-linux-mac-windows @@ -138,7 +127,7 @@ via: https://opensource.com/article/20/3/pip-linux-mac-windows 作者:[Vijay Singh Khatri][a] 选题:[lujun9972][b] 译者:[HankChow](https://github.com/HankChow) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 diff --git a/sources/tech/20191223 Prioritizing simplicity in your Python code.md b/sources/tech/20191223 Prioritizing simplicity in your Python code.md deleted file mode 100644 index 6e54625016..0000000000 --- a/sources/tech/20191223 Prioritizing simplicity in your Python code.md +++ /dev/null @@ -1,59 +0,0 @@ -[#]: collector: (lujun9972) -[#]: translator: (caiichenr) -[#]: reviewer: ( ) -[#]: publisher: ( ) -[#]: url: ( ) -[#]: subject: (Prioritizing simplicity in your Python code) -[#]: via: (https://opensource.com/article/19/12/zen-python-simplicity-complexity) -[#]: author: (Moshe Zadka https://opensource.com/users/moshez) - -Prioritizing simplicity in your Python code -====== -This is the second part of a special series about the Zen of Python -focusing on the third and fourth principles: simplicity and complexity. -![Person reading a book and digital copy][1] - -> "Il semble que la perfection soit atteinte non quand il n'y a plus rien à ajouter, mais quand il n'y plus rien à retrancher." -> -> "It seems that perfection is finally attained not when there is no longer anything to add, but when there is no longer anything to take away." -> —Antoine de Saint-Exupéry, _[Terre des Hommes][2]_, 1939 - -A common concern in programming is the struggle with complexity. It is easy for any programmer to make a program so complicated no expert can debug it or modify it. The [Zen of Python][3] would not be complete if it did not touch on this. - -### Simple is better than complex. - -When it is possible to choose at all, choose the simple solution. Python is rarely in the business of _disallowing_ things. This means it is possible, and even straightforward, to design baroque programs to solve straightforward problems. - -It is worthwhile to remember at each point that simplicity is one of the easiest things to lose and the hardest to regain when writing code. - -This can mean choosing to write something as a function, rather than introducing an extraneous class. This can mean avoiding a robust third-party library in favor of writing a two-line function that is perfect for the immediate use-case. Most often, it means avoiding predicting the future in favor of solving the problem at hand. - -It is much easier to change the program later, especially if simplicity and beauty were among its guiding principles than to load the code down with all possible future variations. - -### Complex is better than complicated. - -This is possibly the most misunderstood principle because understanding the precise meanings of the words is crucial. Something is _complex_ when it is composed of multiple parts. Something is _complicated_ when it has a lot of different, often hard to predict, behaviors. - -When solving a hard problem, it is often the case that no simple solution will do. In that case, the most Pythonic strategy is to go "bottom-up." Build simple tools and combine them to solve the problem. - -This is where techniques like _object composition_ shine. Instead of having a complicated inheritance hierarchy, have objects that forward some method calls to a separate object. Each of those can be tested and developed separately and then finally put together. - -Another example of "building up" is using [singledispatch][4], so that instead of one complicated object, we have a simple, mostly behavior-less object and separate behaviors. - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/19/12/zen-python-simplicity-complexity - -作者:[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/read_book_guide_tutorial_teacher_student_apaper.png?itok=_GOufk6N (Person reading a book and digital copy) -[2]: https://en.wikipedia.org/wiki/Wind,_Sand_and_Stars -[3]: https://www.python.org/dev/peps/pep-0020/ -[4]: https://opensource.com/article/19/5/python-singledispatch diff --git a/translated/tech/20191223 Prioritizing simplicity in your Python code.md b/translated/tech/20191223 Prioritizing simplicity in your Python code.md new file mode 100644 index 0000000000..04b303e702 --- /dev/null +++ b/translated/tech/20191223 Prioritizing simplicity in your Python code.md @@ -0,0 +1,60 @@ +[#]: collector: (lujun9972) +[#]: translator: (caiichenr) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Prioritizing simplicity in your Python code) +[#]: via: (https://opensource.com/article/19/12/zen-python-simplicity-complexity) +[#]: author: (Moshe Zadka https://opensource.com/users/moshez) + +简单是Python编程的第一要则 +====== +本文是 Python 之禅特别系列的第二篇,我们将要关注其中第三与第四条原则:简单与复杂。 + +![Person reading a book and digital copy][1] + +> "It seems that perfection is finally attained not when there is no longer anything to add, but when there is no longer anything to take away." +> +> “完美并非无可增,而是不可减。” +> +> —Antoine de Saint-Exupéry, _[Terre des Hommes][2]_, 1939 + +编程时最常有的考量是与复杂性的斗争,只想写出让旁人无从下手的繁杂代码,对每个程序员来讲都算不上难事。倘若未能触及代码的简繁取舍,那么 [Python 之禅][3] 就有了一角残缺。 + +### 简单胜过复杂 Simple is better than complex + +尚有选择余地时,应该选简单的方案。Python 少有不可为之事,这意味着设计出巴洛克风格(即夸张和不理性)的程序只为解决浅显的问题不仅有可能,甚至会频繁发生。 + +正因如此,编程时应当谨记,代码的简单性是最易丢失,却最难复得的。 + +这意味着,在可以选用函数来表达时不要去引入额外的类;避免使用第三方库往往有助于你针对问题场景设计更妥当的简短函数。不过其根本的意图,旨在让你减少对将来的盘算,而去着重解决手头的问题。 + +以简单和优美作为指导原则的代码相比那些想要囊括将来一切变数的,在日后要容易修改得多。 + +### 复杂胜过错综复杂 Complex is better than complicated + +把握用词的精确含义对于理解这条令人费解的原则是至关重要的。形容某事复杂 (_complex_),是说它由许多部分组成,着重组成成分之多;而形容某事错综复杂 (_complicated_),则是指其包含着差异巨大、难以预料的行为,强调的是各组成部分之间的杂乱联系。 + +解决困难问题时,往往没有可行的简单方案。此时,最 Python 化的策略是“自底向上”地构建出简单的工具,之后将其组合用以解决该问题。 + +这正是对象组合 (_object composition_) 这类技术的闪耀之处,它避免了错综复杂的继承体系,转而由独立的对象把一些方法调用传递给别的独立对象。这些对象都能独立地测试与部署,最终却可以组成一体。 + +“自底建造” 的另一例即是 [单分派泛函数 (singledispatch)][4] 的使用,抛弃了错综复杂的对象之后,我们得到是简单、几乎无行为的对象以及独立的行为。 + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/19/12/zen-python-simplicity-complexity + +作者:[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/read_book_guide_tutorial_teacher_student_apaper.png?itok=_GOufk6N (Person reading a book and digital copy) +[2]: https://en.wikipedia.org/wiki/Wind,_Sand_and_Stars +[3]: https://www.python.org/dev/peps/pep-0020/ +[4]: https://opensource.com/article/19/5/python-singledispatch