SORU
25 Mart 2011, Cuma


Stil Python - dizeleri ile devam hat?

İtaat etmek için python stil kuralları çalışırken, 79 sütunlar için en fazla editörlerime ayarladım.

KEP, parantez, parantez ve parantez içinde python'un ima devamı kullanarak önerir. Col sınırı vurduğumda dizeleri ile uğraşırken, ancak, işler biraz garipleşti.

Örneğin, bir çok satırlı kullanımı

mystr = """Why, hello there
wonderful stackoverflow people!"""

Dönecektir

"Why, hello there\nwonderful stackoverflow people!"

Bu gibi çalışır:

mystr = "Why, hello there \
wonderful stackoverflow people!"

O zamandan beri döner bu

"Why, hello there wonderful stackoverflow people!"

Ama, deyim birkaç blok içinde girintili olduğunda, bu garip görünüyor:

do stuff:
    and more stuff:
        and even some more stuff:
            mystr = "Why, hello there \
wonderful stackoverflow people!"

Eğer denerseniz ve girinti ikinci satır:

do stuff:
    and more stuff:
        and even some more stuff:
            mystr = "Why, hello there \
            wonderful stackoverflow people!"

Eğer string olarak bulur:

"Why, hello there                wonderful stackoverflow people!"

Bunu çözmek için bulduğum tek yolu vardır:

do stuff:
    and more stuff:
        and even some more stuff:
            mystr = "Why, hello there" \
            "wonderful stackoverflow people!"

Daha çok seviyorum, ama aynı zamanda bir dize sadece ıssız bir yerde oturuyor gibi görünüyor gibi gözleri, biraz huzursuz. Bu doğru: üretecektir

"Why, hello there wonderful stackoverflow people!"

Benim sorum ise şu: bazı insanlar bunun nasıl yapılacağı hakkında öneriler ve stil bunu yapmam gerektiğine nasıl şov rehberi kaçırdığım bir şey mi?

Teşekkürler.

CEVAP
25 Mart 2011, Cuma


adjacent string literals are automatically joint into a single string beri sadece KEP 8 tarafından önerilen olarak parantez içinde örtülü satır devamı kullanabilirsiniz:

print("Why, hello there wonderful "
      "stackoverflow people!")

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Official Android Tips

    Official And

    23 EYLÜL 2009
  • BlackmillMusic

    BlackmillMus

    3 Kasım 2010
  • Shanice Caruthers

    Shanice Caru

    27 EKİM 2011