Python对制表符和空格进行缩进的解释 [英] Python's interpretation of tabs and spaces to indent

查看:549
本文介绍了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?

推荐答案

空格不等同于tab.带有制表符的行缩进的行与带有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\n"
    print "yyy\n"

"^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\n"
                ^
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

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\n"
        print "y is unset\n"

在Python 2中,此代码有效,什么也不打印.

In Python 2, this code works, printing nothing.

在Python 3中,规则略有不同(如 Antti Haapala = 1 注释.比较:

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

  • Python 2 on Indentation
  • Python 3 on Indentation

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:

制表符用1到8个空格替换(从左到右),这样,包括替换在内的字符总数是8的倍数(这与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 vs 8空格示例将在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对制表符和空格进行缩进的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆