我应该如何编写很长的代码?

发布于 2024-09-12 17:10:52 字数 371 浏览 3 评论 0原文

如果我有很长的一行代码,是否可以在下一行继续它,例如:

 url='http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|'
+ '100,000|1,000,000&chxp=1,0&chxr=0,0,' +
      max(freq) + '300|1,0,3&chxs=0,676767,13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,300&chd=t:'

if i have a very long line of a code, is it possible to continue it on the next line for example:

 url='http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|'
+ '100,000|1,000,000&chxp=1,0&chxr=0,0,' +
      max(freq) + '300|1,0,3&chxs=0,676767,13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,300&chd=t:'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

信愁 2024-09-19 17:10:52

我会这样写

url=('http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|'
     '100,000|1,000,000&chxp=1,0&chxr=0,0,%(max_freq)s300|1,0,3&chxs=0,676767'
     ',13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465'
     '&cht=bvs&chco=A2C180&chds=0,300&chd=t:'%{'max_freq': max(freq)})

请注意,不需要 + 来连接字符串。这种方式更好,因为字符串是在编译时而不是运行时连接的。

我还在您的字符串中嵌入了 %(max_freq)s ,这是从末尾的 dict 中替换的

另请查看 urllib.urlencode() 如果你想让你的 url 处理更简单

I would write it like this

url=('http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|'
     '100,000|1,000,000&chxp=1,0&chxr=0,0,%(max_freq)s300|1,0,3&chxs=0,676767'
     ',13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465'
     '&cht=bvs&chco=A2C180&chds=0,300&chd=t:'%{'max_freq': max(freq)})

Note that the + are not required to join the strings. It is better this way because the strings are joined at compile time instead of runtime.

I've also embedded %(max_freq)s in your string, this is substituted in from the dict at the end

Also check out urllib.urlencode() if you want to make your url handling simpler

自控 2024-09-19 17:10:52

将来到哪里寻求帮助

大多数像这样的语法问题都在 PEP 8。这个问题的答案可以参考“代码布局”一节。

首选方式:使用 (), {} & []

来自 PEP-8:

换行长行的首选方法是使用 Python 的隐含行
括号、方括号和大括号内的延续。如果有必要的话,你
可以在表达式周围添加一对额外的括号...

这意味着您的示例将像这样:

url= ('http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' +
      '100,000|1,000,000&chxp=1,0&chxr=0,0,' +
      max(freq) + 
      '300|1,0,3&...chco=A2C180&chds=0,300&chd=t:')

替代方法:使用 \

From PEP-8:

...但有时使用反斜杠看起来更好。确保缩进续行
适当地。

url = 'http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' + \
      '100,000|1,000,000&chxp=1,0&chxr=0,0,' + \ 
       max(freq) + \
      '300|1,0,3&...chco=A2C180&chds=0,300&chd=t:'

避免串联

字符串格式

在本例中,我们只想在 URL 中更改一件事:max(freq)。为了有效地将其插入到新字符串中,我们可以使用带有数字或命名参数的 format 方法:

url = "http://...{0}.../".format(max(freq))
url = "http://...{max_freq}.../".format(max_freq=max(freq))

Where to look for help in future

Most syntax problems like this are dealt with in PEP 8. For the answer to this question, you can refer to the section "Code Layout".

Preferred way : Use (), {} & []

From PEP-8:

The preferred way of wrapping long lines is by using Python's implied line
continuation inside parentheses, brackets and braces. If necessary, you
can add an extra pair of parentheses around an expression...

This means your example would like like this:

url= ('http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' +
      '100,000|1,000,000&chxp=1,0&chxr=0,0,' +
      max(freq) + 
      '300|1,0,3&...chco=A2C180&chds=0,300&chd=t:')

The alternate way: Use \

From PEP-8:

...but sometimes using a backslash looks better. Make sure to indent the continued line
appropriately.

url = 'http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' + \
      '100,000|1,000,000&chxp=1,0&chxr=0,0,' + \ 
       max(freq) + \
      '300|1,0,3&...chco=A2C180&chds=0,300&chd=t:'

Avoiding concatenation

String formatting

In this case, we only have a single thing that we would like to be changed in the URL: max(freq). In order to efficiently insert this into a new string, we can use the format method with numerical or named arguments:

url = "http://...{0}.../".format(max(freq))
url = "http://...{max_freq}.../".format(max_freq=max(freq))
少跟Wǒ拽 2024-09-19 17:10:52

Python 将两个字符串文字组合在一起,

>>> s = "abc" "def"
>>> s
'abcdef'

但如果它们位于两行,则不起作用,因为 Python 不知道下一行是命令的一部分。要解决这个问题,您可以使用反斜杠或方括号。

>>> s = ("hello, world"
"!")
>>> s
'hello, world!'

并且您不需要 + 将字符串连接在一起。您仍然需要它来添加像 max(freq) 这样的非文字,如 字符串文字连接。这稍微更高效,但更重要的是更清晰,并且可以对字符串的部分进行注释,如链接的 Python 文档中所示。

Python combines two strings literal together, so

>>> s = "abc" "def"
>>> s
'abcdef'

but that wouldn't work if they are on two lines because Python doesn't know that the next line is part of the command. To solve that you can use backslash or brackets.

>>> s = ("hello, world"
"!")
>>> s
'hello, world!'

and you wouldn't need + to attach the strings together. You will still need it for adding nonliterals like max(freq), as explained in String Literal Concatenation. This is marginally more efficient, but more importantly clearer and enables commenting parts of a string as shown in the Python documentation linked.

生寂 2024-09-19 17:10:52

是的,使用反斜杠 \,如下所示:

url='http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' + \
'100,000|1,000,000&chxp=1,0&chxr=0,0,' + \ 
      max(freq) + '300|1,0,3&chxs=0,676767,13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,300&chd=t:' 

或者您可以用括号 () 将表达式括起来:

url=('http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' +
'100,000|1,000,000&chxp=1,0&chxr=0,0,' +  
      max(freq) + '300|1,0,3&chxs=0,676767,13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,300&chd=t:') 

Yes, use a backslash \, like so:

url='http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' + \
'100,000|1,000,000&chxp=1,0&chxr=0,0,' + \ 
      max(freq) + '300|1,0,3&chxs=0,676767,13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,300&chd=t:' 

Or you can wrap your expression with parentheses ():

url=('http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' +
'100,000|1,000,000&chxp=1,0&chxr=0,0,' +  
      max(freq) + '300|1,0,3&chxs=0,676767,13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,300&chd=t:') 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文