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)。
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)🎜
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 expressionx+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 processy+x
, which will cally.__radd__(x)
.object.__add__(self, other)
这样一个方法。每当处理x+y
这一个表达式的时候,将会调用x.__add__(y)
。另外为了交换律,还有__radd__
方法,也就是用来处理y+x
,这样会调用y.__radd__(x)
。所以我们也可以自己重载
+
,比如:Free
这个Class,便得以直接成为+
操作符的左/右参数。正如richardzhiming所说,
So we can also overloada+b
是低效的,这是基于大量的+
操作所讨论的。这是因为String是一个不可变的对象,你并不是真的把字符串b加到字符串a这个对象后面,而是需要创建一个新的对象,然后把a和b copy进去。因此当你大量进行+
之时,你会创建许许多多临时的String对象。''.join(str_list)
+
ourselves, for example:rrreee
As richardzhiming said,Free
This Class can directly become the left/right parameter of the+
operator.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.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)
就可以看到那些「特殊方法」另,+连接字符串时,时间复杂度太高, 不如
In addition, when + concatenating strings, the time complexity is too high, so it is not as good as thejoin
方法, 比如"".join(iterable)
join
method, such as"".join(iterable)
🎜+This operator provides an overload that takes two strings as parameters