python += 字符串连接是不好的做法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39675898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is python += string concatenation bad practice?
提问by nos
I am reading The Hitchhiker's Guide to Pythonand there is a short code snippet
我正在阅读The Hitchhiker's Guide to Python并且有一个简短的代码片段
foo = 'foo'
bar = 'bar'
foobar = foo + bar # This is good
foo += 'ooo' # This is bad, instead you should do:
foo = ''.join([foo, 'ooo'])
The author pointed out that ''.join()
is not always faster than +
, so he is not against using +
for string concatenation.
作者指出''.join()
并不总是比 快+
,所以他不反对使用+
for string concatenation。
But why is foo += 'ooo'
bad practice whereas foobar=foo+bar
is considered good?
但是为什么foo += 'ooo'
不好的做法foobar=foo+bar
被认为是好的呢?
- is
foo += bar
good? - is
foo = foo + 'ooo'
good?
- 是
foo += bar
好? - 是
foo = foo + 'ooo'
好?
Before this code snippet, the author wrote:
在这段代码片段之前,作者写道:
One final thing to mention about strings is that using join() is not always best. In the instances where you are creating a new string from a pre-determined number of strings, using the addition operator is actually faster, but in cases like above or in cases where you are adding to an existing string, using join() should be your preferred method.
关于字符串的最后一件事是使用 join() 并不总是最好的。在从预定数量的字符串创建新字符串的情况下,使用加法运算符实际上更快,但在上述情况下或在您添加到现有字符串的情况下,使用 join() 应该是您的首选方法。
回答by Ted Klein Bergman
Is it bad practice?
这是不好的做法吗?
It's reasonable to assume that it isn't bad practice for this example because:
可以合理地假设对于这个例子来说这不是坏习惯,因为:
- The author doesn't give any reason. Maybe it's just disliked by him/her.
- Python documentation doesn't mention it's bad practice (from what I've seen).
foo += 'ooo'
is just as readable (according to me) and is approximately 100 times faster thanfoo = ''.join([foo, 'ooo'])
.
- 作者没有给出任何理由。也许只是他/她不喜欢。
- Python 文档没有提到这是不好的做法(根据我所见)。
foo += 'ooo'
与foo = ''.join([foo, 'ooo'])
.
When should one be used over the other?
什么时候应该使用一个?
Concatenation of strings have the disadvantage of needing to create a new string and allocate new memory for every concatenation! This is time consuming, but isn't that big of a deal with few and small strings. When you know the number of strings to concatenate and don't need more than maybe 2-4 concatenations I'd go for it.
字符串连接的缺点是需要创建一个新字符串并为每个连接分配新的内存!这很耗时,但对于很少和小的字符串来说并不是什么大问题。当您知道要连接的字符串数量并且不需要超过 2-4 个连接时,我会去做。
When joining strings Python only has to allocate new memory for the final string, which is much more efficient, but could take longer to compute. Also, because strings are immutable it's often more practical to use a list of strings to dynamically mutate, and only convert it to a string when needed.
连接字符串时,Python 只需为最终字符串分配新内存,这样效率更高,但计算时间可能更长。此外,由于字符串是不可变的,因此使用字符串列表来动态改变通常更实用,并且仅在需要时将其转换为字符串。
It's often convenient to create strings with str.join() since it takes an iterable. For example:
使用 str.join() 创建字符串通常很方便,因为它需要一个可迭代对象。例如:
letters = ", ".join("abcdefghij")
To conclude
总结
In most cases it makes more sense to use str.join()
but there are times when concatenation is just as viable. Using any form of string concatenation for huge or many strings would be bad practice just as using str.join()
would be bad practice for short and few strings, in my own opinion.
在大多数情况下,使用它更有意义,str.join()
但有时串联也同样可行。str.join()
在我看来,对巨大的或许多字符串使用任何形式的字符串连接都是不好的做法,就像对短字符串和少数字符串使用也是不好的做法一样。
I believe that the author was just trying to create a rule of thumb to easier identify when to use what without going in too much detail or make it complicated.
我相信作者只是想创建一个经验法则,以便更容易地确定何时使用什么,而不需要太多细节或使其变得复杂。