簡體   English   中英

Python 多行注釋的 PEP8 / 最佳實踐是什么?

[英]What's the PEP8 / best practice for Python multiline comments?

我使用了很多代碼模式:

1    class myTest():
2        counter = 0      # use this as general counter
3        name_code = ''   # this is the name encoded using the
4                         # standard as defined in ...
5        category = ''    # category of test

第 4 行不是 PEP8 標准。 # 應該是 position 5,恕我直言,這看起來很丑:

3        name_code = ''   # this is the name encoded using the
4        #                  standard as defined in ...

你如何評論這樣的模式? 什么是最佳實踐?

我將記錄 class文檔字符串中的變量,因為它們是 class 的 static 成員。 這樣,您的實際代碼會更緊湊,更易於閱讀和消化:

class myTest():
    """Test class which does something.

    Attributes:
        counter (int): Use this as general counter.
        name_code (str): This is the name encoded using the
            standard as defined in this long piece of text.
            Just continue the lines with one level of indentation
            for as long as you want.
        category (str): Category of the test.

    """

    counter = 0
    name_code = ''
    category = ''

我在這里使用了Google 風格的 Python 文檔字符串,因為這只是我喜歡的風格。

我會將它們作為docstrings進行,因此它們實際上可用於其他工具,例如 Sphinx,並且可以用於生成文檔:

class myTest():
    counter = 0
    """use this as general counter"""

    name_code = ''
    """this is the name encoded using the standard as defined in ..."""

    category = ''
    """category of test"""

文檔字符串通常是多行字符串,因此可以根據需要擴展到多行。

例如, 這里有一個 class,我已經用文檔字符串記錄了它的屬性,允許從它自動生成這個文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM