Ale*_*der 0 python pep8 indentation
基本思想是写这样的一行:
url = 'https://{host}?key={key}&lang={lang}&text='.format(**data) + '&text='.join(words)
Run Code Online (Sandbox Code Playgroud)
想要用PEP 8样式重写它,所以写了这样的代码:
url = 'https://{host}?key={key}&lang={lang}&text='.format(**data) \
+ '&text='.join(words)
Run Code Online (Sandbox Code Playgroud)
其中哪一项是对的?
如果两者都不行,我想听听为什么,然后看看你会怎么写。
都不行 我可能很想尝试为此设计的方法,例如:
from urllib.parse import urlencode
host = 'example.com'
data = {'key': 'foo', 'lang': 'bar', 'text': 'baz'}
url = 'https://{host}?{query}'.format(host=host, query=urlencode(data))
Run Code Online (Sandbox Code Playgroud)
这将为您提供url
:
'https://example.com?key=foo&lang=bar&text=baz'
Run Code Online (Sandbox Code Playgroud)