什么是 Python Whitespace 以及它是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13884499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is Python Whitespace and how does it work?
提问by user1892304
I've been searching google and this website for some time now, but I just can't seem to find a straight answer on the subject.
我一直在搜索谷歌和这个网站一段时间了,但我似乎无法找到关于这个主题的直接答案。
What is whitespace in Python?I know it's something to do with indenting with each line, but I'm not sure exactly how to use it. How does it work?
Python 中的空格是什么?我知道这与每行缩进有关,但我不确定如何使用它。它是如何工作的?
回答by Brigand
Whitespace is used to denote blocks. In other languages curly brackets ({and }) are common. When you indent, it becomes a child of the previous line. In addition to the indentation, the parent also has a colon following it.
空白用于表示块。在其他语言中,大括号 ({和}) 很常见。当您缩进时,它成为上一行的子行。除了缩进,父级后面还有一个冒号。
im_a_parent:
im_a_child:
im_a_grandchild
im_another_child:
im_another_grand_child
Off the top of my head, def, if, elif, else, try, except, finally, with, for, while, and classall start blocks. To end a block, you simple outdent, and you will have siblings. In the above im_a_childand im_another_childare siblings.
在我的头顶上,def, if, elif, else, try, except, finally, with, for, while, 和class所有起始块。结束一个块,你简单的outdent,你会有兄弟姐妹。在上面im_a_child和im_another_child是兄弟姐妹。
回答by Marcin
Whitespace just means characters which are used for spacing, and have an "empty" representation. In the context of python, it means tabs and spaces (it probably also includes exotic unicode spaces, but don't use them). The definitive reference is here: http://docs.python.org/2/reference/lexical_analysis.html#indentation
空白仅表示用于间距的字符,并具有“空”表示。在 python 的上下文中,它意味着制表符和空格(它可能还包括奇异的 unicode 空格,但不要使用它们)。最终参考在这里:http: //docs.python.org/2/reference/lexical_analysis.html#indentation
I'm not sure exactly how to use it.
我不确定如何使用它。
Put it at the front of the line you want to indent. If you mix spaces and tabs, you'll likely see funky results, so stick with one or the other. (The python community usually follows PEP8 style, which prescribes indentation of four spaces).
将其放在要缩进的行的前面。如果您混合使用空格和制表符,您可能会看到奇怪的结果,因此请坚持使用其中之一。(python 社区通常遵循 PEP8 风格,规定缩进四个空格)。
You need to create a new indent level after each colon:
您需要在每个冒号后创建一个新的缩进级别:
for x in range(0, 50):
print x
print 2*x
print x
In this code, the first two printstatements are "inside" the body of the forstatement because they are indented more than the line containing the for. The third printis outside because it is indented less than the previous (nonblank) line.
在此代码中,前两个print语句位于for语句主体的“内部”,因为它们比包含for. 第三个print在外面,因为它比前一个(非空白)行缩进得少。
If you don't indent/unindent consistently, you will get indentation errors. In addition, all compound statements (i.e. those with a colon) can have the body supplied on the same line, so no indentation is required, but the body must be composed of a single statement.
如果您没有一致地缩进/取消缩进,则会出现缩进错误。此外,所有复合语句(即带有冒号的语句)都可以在同一行提供正文,因此不需要缩进,但正文必须由单个语句组成。
Finally, certain statements, like lambdafeature a colon, but cannot have a multiline block as the body.
最后,某些语句,例如lambda具有冒号,但不能以多行块作为主体。
回答by SjayKH
something
{
something1
something2
}
something3
In Python
在 Python 中
Something
something1
something2
something3
回答by Ashwani Arya
It acts as curly bracket. We have to keep the number of white spaces consistent through out the program.
它充当大括号。我们必须在整个程序中保持空格的数量一致。
Example 1:
示例 1:
def main():
print "we are in main function"
print "print 2nd line"
main()
Result:
结果:
We are in main function
print 2nd line
我们在主函数
打印第二行
Example 2:
示例 2:
def main():
print "we are in main function"
print "print 2nd line"
main()
Result:
结果:
print 2nd line
We are in main function
打印第二行
我们在主函数中
Here, in the 1st program, both the statement comes under the main function since both have equal number of white spaces while in the 2nd program, the 1st line is printed later because the main function is called after the 2nd line Note - The 2nd line has no white space, so it is independent of the main function.
在这里,在第一个程序中,两个语句都在 main 函数下,因为它们都有相同数量的空格,而在第二个程序中,第一行稍后打印,因为 main 函数是在第二行之后调用的 注意 - 第二行没有空格,所以它独立于 main 函数。
回答by Roshan
Every programming language has its own way of structuring the code.
whenever you write a block of code, it has to be organised in a way to be understood by everyone.
每种编程语言都有自己的代码结构方式。
每当您编写代码块时,都必须以每个人都能理解的方式对其进行组织。
Usually used in conditional and classes and defining the definition.
It represents the parent, child and grandchild and further.
通常用于条件和类并定义定义。
它代表父母、孩子和孙子,甚至更远。
Example:
例子:
def example()
print "name"
print "my name"
example()
Here you can say example()is a parent and others are children.
在这里你可以说example()是父母,其他人是孩子。

