Python对缩进制表符和空格的解释

时间:2022-11-26
本文介绍了Python对缩进制表符和空格的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我决定学习一点 Python.第一个介绍说它使用缩进来分组语句.虽然最好的习惯显然是只使用其中一种,但如果我互换它们会发生什么?多少个空格将被视为等于一个制表符?或者如果混合使用制表符和空格,它会完全不起作用吗?

I decided, that I learn a bit of Python. The first introduction says that it uses indentation to group statements. While the best habit is clearly to use just one of these what happens if I interchange them? How many spaces will be considered equal to one tab? Or will it fail to work at all if tabs and spaces are mixed?

推荐答案

空格不等同于制表符.以制表符缩进的行与以 1、2、4 个 或 8 个空格缩进的行的缩进不同.

Spaces are not treated as equivalent to tab. A line indented with a tab is at a different indentation from a line indented with 1, 2, 4 or 8 spaces.

通过反例证明(错误,或者,充其量是有限的 - 制表符!= 4 个空格):

x = 1
if x == 1:
^Iprint "fff
"
    print "yyy
"

'^I' 显示一个 TAB.通过 Python 2.5 运行时,出现错误:

The '^I' shows a TAB. When run through Python 2.5, I get the error:

  File "xx.py", line 4
    print "yyy
"
                ^
IndentationError: unindent does not match any outer indentation level

由此表明在 Python 2.5 中,制表符不等于空格(尤其是不等于 4 个空格).

Thus showing that in Python 2.5, tabs are not equal to spaces (and in particular not equal to 4 spaces).

哎呀 - 尴尬;我的反例证明表明制表符不等于 4 个空格.正如 Alex Martelli 在 comment,在 Python 2 中,制表符相当于 8 个空格,并用制表符和8个空格说明确实如此.

Oops - embarrassing; my proof by counter-example shows that tabs are not equivalent to 4 spaces. As Alex Martelli points out in a comment, in Python 2, tabs are equivalent to 8 spaces, and adapting the example with a tab and 8 spaces shows that this is indeed the case.

x = 1
if x != 1:
^Iprint "x is not 1
"
        print "y is unset
"

在 Python 2 中,此代码有效,不打印任何内容.

In Python 2, this code works, printing nothing.

在 Python 3 中,规则略有不同(如 由 Antti Haapala 注明).比较:

In Python 3, the rules are slightly different (as noted by Antti Haapala). Compare:

  • Python 2 缩进
  • Python 3 缩进

Python 2 说:

Python 2 says:

首先,制表符被替换为(从左到右)一到八个空格,这样直到替换的字符总数(包括替换)是八的倍数(这与 Unix 使用的规则相同)).第一个非空白字符之前的空格总数决定了行的缩进.缩进不能使用反斜杠分割成多条物理行;第一个反斜杠之前的空格决定缩进.

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 (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line’s indentation. Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.

Python 3 说:

Python 3 says:

制表符被一到八个空格替换(从左到右),这样直到(包括替换在内)的字符总数是八的倍数(这与 Unix 使用的规则相同).第一个非空白字符之前的空格总数决定了行的缩进.缩进不能使用反斜杠分割成多条物理行;第一个反斜杠之前的空格决定缩进.

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 (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line’s indentation. Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.

(除了开头的单词First"之外,它们是相同的.)

(Apart from the opening word "First," these are identical.)

Python 3 增加了一个额外的段落:

Python 3 adds an extra paragraph:

如果源文件混合制表符和空格的方式使含义取决于空格中制表符的价值,则缩进会被拒绝;在这种情况下会引发 TabError.

Indentation is rejected as inconsistent if a source file mixes tabs and spaces in a way that makes the meaning dependent on the worth of a tab in spaces; a TabError is raised in that case.

这意味着在 Python 2 中工作的 TAB 与 8-space 示例将在 Python 3 中生成 TabError.最好(在 Python 3 中是必需的)确保字符序列块中每一行的缩进是相同的.PEP8 说每个缩进级别使用 4 个空格".(Google 的编码标准说使用 2 个空格".)

This means that the TAB vs 8-space example that worked in Python 2 would generate a TabError in Python 3. It is best — necessary in Python 3 — to ensure that the sequence of characters making up the indentation on each line in a block is identical. PEP8 says 'use 4 spaces per indentation level'. (Google's coding standards say 'use 2 spaces'.)

这篇关于Python对缩进制表符和空格的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:二进制文件电子邮件附件问题 下一篇:如何在 tkinter 应用程序中设置选项卡顺序?

相关文章

最新文章