5

PEP8 とスタック オーバーフローに関するいくつかの質問を読んでいましたが、コメント間のスペースについて疑問に思っていました。

このコードがあるとしましょう:

class MyBrowser(QWebPage):
    ''' Settings for the browser.'''

    def __init__(self):
        QWebPage.__init__(self)
        # Specifies whether images are automatically loaded in web pages.
        self.settings().setAttribute(QWebSettings.AutoLoadImages, True)

    def userAgentForUrl(self, url):
        ''' Returns a User Agent that will be seen by the website. '''
        return "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15"

コメントと実際のコードの間に空白行を入れる最も Pythonic な方法は何ですか? 私のプログラムを何人かの専門家に見せたいです。そして、私のコードをよりプロフェッショナルに見せたいです。

4

4 に答える 4

12

これが「コミュニティ標準」を表しているかどうかはわかりませんが、ここにGoogle の Python スタイル ガイドがあります(コメントに関連しているため)。具体的にはクラス:

class SampleClass(object):
    """Summary of class here.

    Longer class information....
    Longer class information....

    Attributes:
        likes_spam: A boolean indicating if we like SPAM or not.
        eggs: An integer count of the eggs we have laid.
    """

    def __init__(self, likes_spam=False):
        """Inits SampleClass with blah."""
        self.likes_spam = likes_spam
        self.eggs = 0

    def public_method(self):
        """Performs operation blah."""
于 2012-12-12T22:58:49.300 に答える
2

疑わしい場合は、モデルの標準ライブラリを参照してください。

これはtimeitモジュールからの抜粋です (Guido van Rossum 自身によって書かれました):

def print_exc(self, file=None):
    """Helper to print a traceback from the timed code.

    Typical use:

        t = Timer(...)       # outside the try/except
        try:
            t.timeit(...)    # or t.repeat(...)
        except:
            t.print_exc()

    The advantage over the standard traceback is that source lines
    in the compiled template will be displayed.

    The optional file argument directs where the traceback is
    sent; it defaults to sys.stderr.
    """
    import linecache, traceback
    if self.src is not None:
        linecache.cache[dummy_src_name] = (len(self.src),
                                           None,
                                           self.src.split("\n"),
                                           dummy_src_name)
    # else the source is already stored somewhere else

    traceback.print_exc(file=file)
于 2012-12-12T22:57:48.427 に答える
1

PythonのZenから:「読みやすさは重要です。」あなたのチームが最も読みやすいと思うものは何でも私がすることです。

于 2012-12-12T22:54:35.863 に答える