問題:
How do I wrap long lines in Python without sacrificing indentation?如何在不犧牲縮進的情況下在 Python 中包裝長行?
For example:例如:
def fun():
print '{0} Here is a really long sentence with {1}'.format(3, 5)
Suppose this goes over the 79 character recommended limit.假設這超過了 79 個字符的推薦限制。 The way I read it, here is how to indent it:我閱讀它的方式,這裏是如何縮進它:
def fun():
print '{0} Here is a really long \
sentence with {1}'.format(3, 5)
However, with this approach, the indentation of the continued line matches the indentation of the fun()
.但是,使用這種方法,連續行的縮進與fun()
的縮進相匹配。 This looks kinda ugly.這看起來有點醜。 If someone was to go through my code, it would look bad to have uneven indentation because of this print
statement.如果有人要查看我的代碼,由於此print
語句,縮進不均勻會看起來很糟糕。
How do I indent lines like this effectively without sacrificing code readability?如何在不犧牲代碼可讀性的情況下有效地縮進這樣的行?
解決方案:
參考一: https://stackoom.com/question/E2VS參考二: Wrap long lines in Python [duplicate]