Python 缩进

发布于 2024-11-03 03:01:35 字数 476 浏览 4 评论 0原文

我认为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

古镇旧梦 2024-11-10 03:01:35

简而言之,只要有左括号或方括号或大括号,语句就可以继续到下一行。

有效:

print ("hello " 
        "world"
        "!")

无效:

print "hello " 
       "world"
       "!"

Put simply, a statement can continue on to the next line as long as there is an open parenthesis or bracket or brace.

Valid:

print ("hello " 
        "world"
        "!")

Invalid:

print "hello " 
       "world"
       "!"
幸福%小乖 2024-11-10 03:01:35

当您打开括号(无论是圆形还是方形)时,所有标识都会被忽略,直到您关闭该括号:

print(1,
           15,
  4)

完全等于

print(1, 15, 4)

The second you open a bracket (either round or square) all identation is ignored until you close that one:

print(1,
           15,
  4)

is perfectly equal to

print(1, 15, 4)
轻许诺言 2024-11-10 03:01:35

直到所有 () 都匹配后,该行才会关闭。也适用于 [] 和 {}

The line isn't closed until all the () are matched. Also works for [] and {}

恋你朝朝暮暮 2024-11-10 03:01:35

不,语句可以在多种情况下继续。在这种情况下,参数列表中存在左括号意味着语句在括号关闭之前不会结束。对于标记列表、元组、字典的开头和结尾的括号和括号也是如此。

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.

疾风者 2024-11-10 03:01:35

不,如果有括号换行,则允许在 2 行上写一条语句。例如我们可以写:

a = ('1' + '2' + '3' +
    '4' + '5')

No, it's allowed to write a statement on 2 line if there's the bracket wrapping. For example we can write:

a = ('1' + '2' + '3' +
    '4' + '5')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文