我正在阅读PEP8以及有关stackoverflow的一些问题,但是想知道评论之间的空格:
假设我有这段代码:
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方法是什么?我想向一些专家展示我的计划。并希望我的代码看起来更专业。
答案 0 :(得分:12)
我不知道这是否代表“社区标准”,但这里是Google's Python style guides(因为它们与评论有关)。特别是班级:
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."""
答案 1 :(得分: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)
答案 2 :(得分:1)
来自Python的禅宗:“可读性很重要。”无论你的团队发现哪个是最可读的,我都会做。
答案 3 :(得分:0)
不是使用片段,而是使用sphinx查看最常用的cpython,并将文档与代码进行比较。
https://docs.python.org/3/library/difflib.html
https://github.com/python/cpython/blob/3.7/Lib/difflib.py
由于注释位于代码内部,因此文档永远不会同步。