PEP-8用于函数调用

yam*_*amm 3 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)
                                            })
                            )
Run Code Online (Sandbox Code Playgroud)

Mic*_*x2a 6

尝试下载代码样式链接,例如pep8(检查代码以查看它是否符合PEP 8要求的程序)或pylint.你可以在这里找到一个更全面的Python样式检查器列表和比较:Python 的综合lint检查器是什么?

事实上,网上有一个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 
Run Code Online (Sandbox Code Playgroud)

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

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

从本质上讲,你所受到的主要PEP 8违规行为是你过多地缩进.单个缩进很好 - 您不需要对齐函数调用的开头数.Python也坚持你的行不超过80个字符,但修复过度缩进也解决了这个问题.