python - 用于函数调用的 PEP-8

标签 python conventions pep8 code-conversion

<分区>

我最近开始使用 Python/Django,并且听说了 PEP 8 约定。看完PEP8我对如何“设计”我的代码有了更好的认识,但我学习了 Java 编程,而且我过去只是做我喜欢做的事。你能建议如何将我的示例放入 PEP-8 中吗?非常感谢。

result = urllib.urlretrieve(
                            "https://secure.gravatar.com/avatar.php?"+
                            urllib.urlencode({
                                            'gravatar_id': hashlib.md5(email).hexdigest(),
                                            'size': srt(size)
                                            })
                            )

最佳答案

尝试下载代码样式 linters,例如 pep8(一种检查您的代码以查看其是否符合 PEP 8 要求的程序)或 pylint。您可以在此处找到更全面的 Python 样式检查器列表和比较:What are the comprehensive lint checkers for Python?

事实上,网上有一个 pep8 检查器:http://pep8online.com/

如果我们通过它运行您的代码,它会告诉您:

Code    Line  Column    Text 
E126    2     29        continuation line over-indented for hanging indent
E225    2     70        missing whitespace around operator
E126    4     45        continuation line over-indented for hanging indent
E501    4     80        line too long (90 > 79 characters)
W292    7     30        no newline at end of file 

您的代码的固定版本看起来更像这样:

result = urllib.urlretrieve(
    "https://secure.gravatar.com/avatar.php?" +
    urllib.urlencode({
        'gravatar_id': hashlib.md5(email).hexdigest(),
        'size': srt(size)
    })
)

本质上,您违反 PEP 8 的主要行为是缩进过多。单个缩进就可以了——您不需要与函数调用的左括号对齐。 Python 还坚持您的行不要超过 80 个字符,但修复过度缩进也解决了该问题。

关于python - 用于函数调用的 PEP-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25725336/

相关文章:

python - 如何动态导入模块?

c++ - 标准 C 函数 : Check for -1 or 0?

python - 关于将 bool 值与 True 或 False 进行比较的奇怪 PEP8 建议

python - 什么是尾随空格,我该如何处理?

python matplotlib Canvas 未调整大小

python - 将brewer2mpl发散颜色图与matplotlib结合使用,gamma 给出的结果很差,其值不同于 1

python - 如何在 Azure Redhat Linux VM 上更新 python?

python - 从子模块导入 Python 包时避免 pylint 投诉

ios - Objective-C 中带下划线前缀的变量名是什么意思?

python - docstring max line-length 是否与正常的 PEP8 标准不同?