爲了服從python樣式規則,我將編輯器設置爲最多79列。Python風格 - 字符串延續?
在PEP中,它建議在括號,圓括號和大括號中使用python的隱含延續。但是,當我遇到字符串限制時處理字符串時,會變得有點奇怪。
例如,試圖用一個多
mystr = """Why, hello there
wonderful stackoverflow people!"""
將返回
"Why, hello there\nwonderful stackoverflow people!"
這工作:
mystr = "Why, hello there \
wonderful stackoverflow people!"
因爲它返回:
"Why, hello there wonderful stackoverflow people!"
但是,當語句中縮進幾個街區,這看起來奇怪:
do stuff:
and more stuff:
and even some more stuff:
mystr = "Why, hello there \
wonderful stackoverflow people!"
如果你試圖縮進第二行:
do stuff:
and more stuff:
and even some more stuff:
mystr = "Why, hello there \
wonderful stackoverflow people!"
你的字符串結束爲:
"Why, hello there wonderful stackoverflow people!"
我發現解決這個問題的唯一方法是:
do stuff:
and more stuff:
and even some more stuff:
mystr = "Why, hello there" \
"wonderful stackoverflow people!"
我喜歡哪一種更好,但眼睛也有點不安,因爲它看起來像是坐在無處不在的中間。這將產生正確的:
"Why, hello there wonderful stackoverflow people!"
所以,我的問題是 - 什麼是不顯示我應該怎麼做這個有些人對如何做到這一點,是有什麼我失蹤的風格指南中的建議?
謝謝。
高縮進級別也可能是你需要重構代碼,以便它更模塊化 – Daenyth 2011-03-25 20:18:38
我縮進那麼多做出點的標誌。但是要意識到,至少可以達到第三級縮進是很容易的,但是即使只有一個縮進級別,標準方法也會使字符串變得非常不合適。 – sjmh 2011-03-25 20:30:35
可能重複的[在Python中換行長行](http://stackoverflow.com/questions/3346230/wrap-long-lines-in-python) – JrBenito 2016-09-27 19:18:06