为什么Python 可以使用 + 把两个字符串连接成一个字符串
伊谢尔伦
伊谢尔伦 2017-04-17 16:14:35
0
3
664

今天学习Python,看到这样一个例子,想问一下原因:

w = "This is the left side of..."
e = "a string with a right side."

print w + e

结果是:

This is the left side of...a string with a right side.

我想问一下为什么通过使用 + 可以把两个字符串连成一个长字符串,用的是什么方法?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(3)
Peter_Zhu

Operator overloading. Specifically, every variable exists in the form of an object in Python, that is, it inherits from Object. Object has a method such as object.__add__(self, other). Whenever the expression x+y is processed, x.__add__(y) will be called. In addition, for the commutative law, there is also the __radd__ method, which is used to process y+x, which will call y.__radd__(x). object.__add__(self, other)这样一个方法。每当处理x+y这一个表达式的时候,将会调用x.__add__(y)。另外为了交换律,还有__radd__方法,也就是用来处理y+x,这样会调用y.__radd__(x)

所以我们也可以自己重载+,比如:

class Free:
    def __init__(self, info):
        self.info = info   
    def __add__(self, moreinfo):
        return str(self) + moreinfo
    def __radd__(self, moreinfo):
        return moreinfo + str(self)
    def __str__(self):
        return self.info

Free这个Class,便得以直接成为+操作符的左/右参数。

正如richardzhiming所说,a+b是低效的,这是基于大量的+操作所讨论的。这是因为String是一个不可变的对象,你并不是真的把字符串b加到字符串a这个对象后面,而是需要创建一个新的对象,然后把a和b copy进去。因此当你大量进行+之时,你会创建许许多多临时的String对象。''.join(str_list)

So we can also overload + ourselves, for example:

rrreee
Free This Class can directly become the left/right parameter of the + operator.

As richardzhiming said, a+b is inefficient, which is discussed based on a large number of + operations. This is because String is an immutable object. You do not really add string b to the object string a. Instead, you need to create a new object and then copy a and b into it. So when you do a lot of +, you will create a lot of temporary String objects. ''.join(str_list) is better because the creation and copying of the string is completed at one time without creating many String objects.

For specific experiments and more in-depth discussions on string concatenation, please refer to: 🎜Efficient String Concatenation in Python🎜The Python String Concatenation Shootout🎜 🎜In addition, the key to program optimization is not to optimize every detail, but to optimize the overall bottleneck, so the question subject does not have to remember every subtle optimization technique. But understanding the specific implementation details is very meaningful for mastering the language. 🎜
迷茫

What is actually called is the so-called special method, in this case it is __add__
dir(str) and you can see those " Special method"特殊方法, 此例中是__add__
dir(str)就可以看到那些「特殊方法」

另,+连接字符串时,时间复杂度太高, 不如join方法, 比如"".join(iterable)

In addition, when + concatenating strings, the time complexity is too high, so it is not as good as the join method, such as "".join(iterable)🎜
伊谢尔伦

+This operator provides an overload that takes two strings as parameters

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!