在 python 2.6 中,为什么以下行有效?
my_line = 'foo' 'bar'
如果这是有效的,为什么以下不是:
my_list = 1 2
第一个例子是字符串连接,然而,下面的例子也是无效的(谢天谢地):
foo = 'foo'
bar = 'bar'
foo_bar = foo bar
最佳答案
这是在做 string literal concatenation .如文档中所述,优点包括:
This feature can be used to reduce the number of backslashes needed, to split long strings conveniently across long lines, or even to add comments to parts of strings...
它接着指出,这种串联是在编译时而不是运行时完成的。
这背后的历史和基本原理,以及被拒绝的删除该功能的建议,在 PEP 3126 中有所描述。 .
关于python - 语法怪癖或为什么有效的 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1940710/