简体   繁体   English

Python缩进

[英]Python Indentation

I thought every line in pythton is a statment, but have a look below: 我认为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)))

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. 在下一行中写入valign = p.CENTER 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? 它还在另一行中写了p.Column,但它与p.Row具有相同的缩进量,这是规则吗?

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"
       "!"

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)

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. 方括号和括号标记列表,元组,字典的开始和结束也是如此。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM