Python 函数参数列表格式
根据 PEP8 格式化以下代码的最佳方法是什么:
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer,
token=token, verifier=verifier, http_url=ACCESS_TOKEN_URL)
问题是,如果我在第一行放置多个参数,则该行超过 79 个字符。如果我将每个参数放在带有 4 个空格缩进的单独行上,它看起来相当难看:
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
consumer,
token=token,
verifier=verifier,
http_url=ACCESS_TOKEN_URL)
我想出的最佳选择是添加额外的缩进以更好地区分:
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
consumer,
token=token,
verifier=verifier,
http_url=ACCESS_TOKEN_URL)
我尝试制定一个使用它的一般规则对于第一行调用较长且多个参数无法容纳一行的方法。
What is the best way to format following piece of code accordingly to PEP8:
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer,
token=token, verifier=verifier, http_url=ACCESS_TOKEN_URL)
The problem is that if I place more than one parameter on the first line, the line exceeds 79 characters. If I place each of the parameters on a separate line with 4 spaces indentation it looks quite ugly:
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
consumer,
token=token,
verifier=verifier,
http_url=ACCESS_TOKEN_URL)
The best option that I come up with is to add extra indentation for better distinguishing:
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
consumer,
token=token,
verifier=verifier,
http_url=ACCESS_TOKEN_URL)
I try to work out a general rule for me to use it for methods with long invocation on the first line and several parameters that can't fit a single line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 PEP 8 的阅读表明 2 和 3 都是可以接受的,但看起来like 2 是首选(我这么说是因为看起来 2 vs. 3 在示例中是这样处理的,我不认为这里的样式规范非常具体)。 1 已退出(请参阅“不使用垂直对齐时禁止第一行的参数”行下的文档)。
My reading of PEP 8 suggests that 2 and 3 are both acceptable, but it looks like 2 is preferred (I say this because it looks like 2 vs. 3 is handled this way in the examples, I don't think that the style specification is very specific here). 1 is out (look at the docs under the line "Arguments on first line forbidden when not using vertical alignment").