python += 字符串连接是不好的做法吗? [英] Is python += string concatenation bad practice?

查看:70
本文介绍了python += 字符串连接是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读搭便车指南Python 并且有一个简短的代码片段

I am reading The Hitchhiker’s Guide to Python and there is a short code snippet

foo = 'foo'
bar = 'bar'

foobar = foo + bar  # This is good
foo += 'ooo'  # This is bad, instead you should do:
foo = ''.join([foo, 'ooo'])

作者指出''.join()并不总是比+快,所以他不反对使用+字符串连接.

The author pointed out that ''.join() is not always faster than +, so he is not against using + for string concatenation.

但是为什么 foo += 'ooo' 是不好的做法而 foobar=foo+bar 被认为是好的?

But why is foo += 'ooo' bad practice whereas foobar=foo+bar is considered good?

  • foo += bar 好吗?
  • foo = foo + 'ooo' 好吗?
  • is foo += bar good?
  • is foo = foo + 'ooo' good?

在这段代码之前,作者写道:

Before this code snippet, the author wrote:

关于字符串的最后一件事是使用 join() 并不总是最好的.在从预定数量的字符串创建新字符串的情况下,使用加法运算符实际上更快,但在上述情况下或在您添加到现有字符串的情况下,使用 join() 应该是您的首选方法.

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.

推荐答案

这是不好的做法吗?

可以合理地假设本示例的做法还不错,因为:

Is it bad practice?

It's reasonable to assume that it isn't bad practice for this example because:

  • 作者没有给出任何理由.也许只是他/她不喜欢.
  • Python 文档没有提到这是不好的做法(据我所知).
  • foo += 'ooo' 具有同样的可读性(根据我的说法)并且比 foo = ''.join([foo, 'ooo']) 快大约 100 倍.
  • 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 than foo = ''.join([foo, 'ooo']).

字符串连接的缺点是需要创建一个新字符串并为每个连接分配新的内存!这很耗时,但对于很少和小的字符串来说并不是什么大问题.当您知道要连接的字符串数量并且不需要超过 2-4 个连接时,我会去做.

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.

连接字符串时,Python 只需为最终字符串分配新内存,这样效率更高,但计算时间可能更长.此外,由于字符串是不可变的,因此使用字符串列表来动态改变通常更实用,并且仅在需要时将其转换为字符串.

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.

使用 str.join() 创建字符串通常很方便,因为它需要一个可迭代对象.例如:

It's often convenient to create strings with str.join() since it takes an iterable. For example:

letters = ", ".join("abcdefghij")

总结

在大多数情况下,使用 str.join() 更有意义,但有时连接也同样可行.在我看来,对巨大或许多字符串使用任何形式的字符串连接都是不好的做法,就像使用 str.join() 对于短字符串和少数字符串是不好的做法一样.

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.

我相信作者只是想创建一个经验法则,以便更容易地确定何时使用什么,而不需要太多细节或使其变得复杂.

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.

这篇关于python += 字符串连接是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆