Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

Concatenation using the + and += operators in Python


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The basic idea is that the + and the += operators are not necessarily the same operation in Python, and they are indeed different for lists. The + operation is carried out by the __add__ magic method while the += operation is carried out by the __iadd__ (in-place add) magic method. These magic methods come from the type on the left of the operator.

The in-place adding for a list does not require a list on the right-hand side, just an iterable. The items are then taken one-by-one from the iterable and appended to the list. This is similar to the list's extend method. So the += operator does not neglect the type of the object on the right, it just extends the possible types that can be used.

This behavior is somewhat confusing--it must be since you are the second person in two days I have seen ask a similar (but not duplicate) question on this issue. However, this behavior is convenient--we now have an easy way to combine a list with any iterable.

For more on this, see Why do Python lists let you += a tuple, when you can’t + a tuple?.


As @khelwood's comment states, the resulting type for a += b is obvious: the type of a. So the type of b can be flexible. The resulting type of a + b is not obvious. Python is a strictly-typed language and hates such ambiguity. The Zen of Python states

Explicit is better than implicit.

and

In the face of ambiguity, refuse the temptation to guess.

and

Although practicality beats purity.

So the current behavior fits Python's Zen pretty well. I note that there is nothing in the Zen about consistency.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...