From af423bc05f3e59b2ee83c7eec956445fc192e3a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9A=AE=E6=99=A8?= <362864550@qq.com> Date: Wed, 21 Nov 2018 22:38:12 +0800 Subject: [PATCH] EX.Well, something is fishy --- README.md | 22 +++++++++++----------- mixed_tabs_and_spaces.py | 7 +++++++ 2 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 mixed_tabs_and_spaces.py diff --git a/README.md b/README.md index 9387f9b..88f857f 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ So, here we go... - [Section: Appearances are deceptive!](#section-appearances-are-deceptive) - [> Skipping lines?/跳过一行?](#-skipping-lines跳过一行) - [> Teleportation/空间移动 *](#-teleportation空间移动-) - - [> Well, something is fishy...](#-well-something-is-fishy) + - [> Well, something is fishy.../嗯, 有些可疑...](#-well-something-is-fishy嗯有些可疑) - [Section: Watch out for the landmines!](#section-watch-out-for-the-landmines) - [> Modifying a dictionary while iterating over it](#-modifying-a-dictionary-while-iterating-over-it) - [> Stubborn `del` operator *](#-stubborn-del-operator-) @@ -1302,12 +1302,12 @@ def energy_receive(): --- -### > Well, something is fishy... +### > Well, something is fishy.../嗯,有些可疑... ```py def square(x): """ - A simple function to calculate the square of a number by addition. + 一个通过加法计算平方的简单函数. """ sum_so_far = 0 for counter in range(x): @@ -1322,17 +1322,17 @@ def square(x): 10 ``` -Shouldn't that be 100? +难道不应该是100吗? -**Note:** If you're not able to reproduce this, try running the file [mixed_tabs_and_spaces.py](/mixed_tabs_and_spaces.py) via the shell. +**注意:** 如果你无法重现, 可以尝试运行这个文件[mixed_tabs_and_spaces.py](/mixed_tabs_and_spaces.py). -#### 💡 Explanation +#### 💡 说明: -* **Don't mix tabs and spaces!** The character just preceding return is a "tab", and the code is indented by multiple of "4 spaces" elsewhere in the example. -* This is how Python handles tabs: - > First, tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight <...> -* So the "tab" at the last line of `square` function is replaced with eight spaces, and it gets into the loop. -* Python 3 is kind enough to throw an error for such cases automatically. +* **不要混用制表符(tab)和空格(space)!** 在上面的例子中, return 的前面是"1个制表符", 而其他部分的代码前面是 "4个空格". +* Python是这么处理制表符的: + > 首先, 制表符会从左到右依次被替换成8个空格, 直到被替换后的字符总数是八的倍数 <...> +* 因此, `square` 函数最后一行的制表符会被替换成8个空格, 导致return语句进入循环语句里面. +* Python 3 很友好, 在这种情况下会自动抛出错误. **Output (Python 3.x):** ```py diff --git a/mixed_tabs_and_spaces.py b/mixed_tabs_and_spaces.py new file mode 100644 index 0000000..ec6cf40 --- /dev/null +++ b/mixed_tabs_and_spaces.py @@ -0,0 +1,7 @@ +def square(x): + sum_so_far = 0 + for _ in range(x): + sum_so_far += x + return sum_so_far # noqa: E999 # pylint: disable=mixed-indentation Python 3 will raise a TabError here + +print(square(10))