Python 缩进
我认为 pythton 中的每一行都是一个语句,但请看下面:
class Report(p.Report):
def create(self):
self.set(background=sp.LightYellow)
self.add(p.Row(p.Text("Trip Name",
valign=p.CENTER,
font=p.font(weight=p.BOLD)),
p.Column(p.Text("Costs",
align=p.CENTER)))
我认为除非语句结束,否则我们不允许到达下一行。它在下一行写入valign = p.CENTER
。怎么可能呢?我们怎样才能换行并在下一行继续该语句呢? 另外一行也写了p.Column,但是和p.Row有同样的缩进,是不是有规律?
I thought every line in pythton is a statment, but have a look below:
class Report(p.Report):
def create(self):
self.set(background=sp.LightYellow)
self.add(p.Row(p.Text("Trip Name",
valign=p.CENTER,
font=p.font(weight=p.BOLD)),
p.Column(p.Text("Costs",
align=p.CENTER)))
I thought we are not allowed to get to next line unless the statement is ended. It writes valign = p.CENTER
in the next line. How is it possible? How can we break a line and continue the statement in the next line?
It has also written p.Column in another line, but it has the same indent with p.Row, is it a rule?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简而言之,只要有左括号或方括号或大括号,语句就可以继续到下一行。
有效:
无效:
Put simply, a statement can continue on to the next line as long as there is an open parenthesis or bracket or brace.
Valid:
Invalid:
当您打开括号(无论是圆形还是方形)时,所有标识都会被忽略,直到您关闭该括号:
完全等于
The second you open a bracket (either round or square) all identation is ignored until you close that one:
is perfectly equal to
直到所有 () 都匹配后,该行才会关闭。也适用于 [] 和 {}
The line isn't closed until all the () are matched. Also works for [] and {}
不,语句可以在多种情况下继续。在这种情况下,参数列表中存在左括号意味着语句在括号关闭之前不会结束。对于标记列表、元组、字典的开头和结尾的括号和括号也是如此。
No, statements can continue across in several cases. In this case, the presence of an open parentheses for an argument list means that the statement doesn't end until the parentheses closes. The same is true for brackets and parens marking the beginning and end of lists, tuples, dicts.
不,如果有括号换行,则允许在 2 行上写一条语句。例如我们可以写:
No, it's allowed to write a statement on 2 line if there's the bracket wrapping. For example we can write: