243

PEP8 が python プログラムの 80 列ルールを下回ることを提案しているように、長い文字列でそれをどのように守ることができますか?

s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten."

これを次の行に展開するにはどうすればよいですか。

s = "this is my really, really, really, really, really, really" + 
    "really long string that I'd like to shorten."
4

13 に答える 13

346

隣接する文字列定数は自動的に連結されるため、次のようにコーディングできます。

s = ("this is my really, really, really, really, really, really, "  
     "really long string that I'd like to shorten.")

プラス記号がないことに注意してください。例の書式設定に続いて、余分なコンマとスペースを追加しました。

個人的に私はバックスラッシュが好きではありません. どこかで読んだことを思い出します. 「明示的は暗黙的よりも優れている」ことを覚えておいてください。

これは実際に改行文字をエスケープしているため、バックスラッシュはあまり明確ではなく、あまり役に立たないと思います。必要に応じて、行末コメントをその後に置くことはできません。連結された文字列定数を使用してこれを行うことができます。

s = ("this is my really, really, really, really, really, really, " # comments ok
     "really long string that I'd like to shorten.")

最初の結果として PEP8 リンクを返す「python line length」の Google 検索を使用しましたが、このトピックに関する別の適切な StackOverflow 投稿へのリンクも使用しました

もう 1 つの適切な検索フレーズは、「python line continuation」です。

于 2009-12-09T15:31:49.670 に答える
138

暗黙の連結が最もク​​リーンな解決策かもしれません:

s = "this is my really, really, really, really, really, really," \
    " really long string that I'd like to shorten."

編集振り返ってみると、行継続ではなく括弧を使用するというトッドの提案は、彼が与えるすべての理由から優れていることに同意します。私が唯一ためらっているのは、角かっこで囲まれた文字列とタプルを比較的混同しやすいということです。

于 2009-12-09T15:25:52.410 に答える
17

あなたの質問で最も重要な言葉は「提案」だったと思います。

コーディング標準は面白いものです。多くの場合、それらが提供するガイダンスは、書かれた時点では非常に優れた基礎を持っていますが (たとえば、ほとんどの端末では 1 行に 80 文字を超える文字を表示することができません)、時間の経過とともに機能的に時代遅れになりますが、依然として固執しています。ここで行う必要があるのは、その特定の提案を「破る」ことの相対的なメリットを、コードの読みやすさと保守性に対して比較検討することだと思います。

申し訳ありませんが、これはあなたの質問に直接答えるものではありません。

于 2009-12-09T15:24:55.927 に答える
16

スペースを失いました。おそらく行継続文字が必要です。\。_

s = "this is my really, really, really, really, really, really" +  \
    " really long string that I'd like to shorten."

あるいは:

s = "this is my really, really, really, really, really, really"  \
    " really long string that I'd like to shorten."

行の継続の代わりに括弧も機能しますが、タプルを使用するつもりでコンマを忘れただけだと誰かに思われる危険があります。たとえば、次のようにします。

s = ("this is my really, really, really, really, really, really"
    " really long string that I'd like to shorten.")

対:

s = ("this is my really, really, really, really, really, really",
    " really long string that I'd like to shorten.")

Python の動的型付けでは、コードはどちらの方法でも実行できますが、意図しない結果で誤った結果が生成されます。

于 2009-12-09T15:24:16.223 に答える
3

バックスラッシュ:

s = "this is my really, really, really, really, really, really" +  \
    "really long string that I'd like to shorten."

または括弧で囲みます:

s = ("this is my really, really, really, really, really, really" + 
    "really long string that I'd like to shorten.")
于 2009-12-09T15:24:01.977 に答える
3

これらはすべて素晴らしい答えですが、「暗黙的に連結された」文字列を編集するのに役立つエディター プラグインが見つからなかったため、簡単にするためのパッケージを作成しました。

この古いスレッドをさまよっている誰かがそれをチェックしたい場合は、pip (段落のインストール) で。複数行の文字列を html と同じようにフォーマットします (空白を圧縮し、新しい段落に 2 つの改行を入れ、行間のスペースを気にしません)。

from paragraphs import par


class SuddenDeathError(Exception):
    def __init__(self, cause: str) -> None:
        self.cause = cause

    def __str__(self):
        return par(
            f""" Y - e - e - e - es, Lord love you! Why should she die of
            {self.cause}? She come through diphtheria right enough the year
            before. I saw her with my own eyes. Fairly blue with it, she
            was. They all thought she was dead; but my father he kept ladling
            gin down her throat till she came to so sudden that she bit the bowl
            off the spoon. 

            What call would a woman with that strength in her have to die of
            {self.cause}? What become of her new straw hat that should have
            come to me? Somebody pinched it; and what I say is, them as pinched
            it done her in."""
        )


raise SuddenDeathError("influenza")

になる...

__main__.SuddenDeathError: Y - e - e - e - es, Lord love you! Why should she die of influenza? She come through diphtheria right enough the year before. I saw her with my own eyes. Fairly blue with it, she was. They all thought she was dead; but my father he kept ladling gin down her throat till she came to so sudden that she bit the bowl off the spoon.

What call would a woman with that strength in her have to die of influenza? What become of her new straw hat that should have come to me? Somebody pinched it; and what I say is, them as pinched it done her in.

(Vim) 'gq' ですべてが簡単に揃う

于 2019-07-08T18:49:48.493 に答える
0

を使用すると、\ステートメントを複数行に展開できます。

s = "this is my really, really, really, really, really, really" + \
"really long string that I'd like to shorten."

動作するはずです。

于 2009-12-09T15:24:08.907 に答える