+ 3

Shocking in-place operators 🔥🔥🔥🔥😱😱👌

Recently I encountered this... (But can't tell why in-place operators work that way) Firstly without in-place operator >>>list=[] >>>list=list+"hello" >>>print(list) TypeError: can only concatenate list(not str) with list Now with in-place operator >>>list=[] >>>list+="hello" >>>print(list) ["h", "e", "l", "l", "o"] So list+="hello" is not the same as saying list=list+"hello"

4th Jul 2018, 9:51 AM
Kallu Dada
2 Antworten
4th Jul 2018, 7:58 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
Adding a list with a string and then assigning it back to a list, is different from appending a string to a list. While x += 5 is commonly explained to be a shorthand of x = x + 5 which is true, different languages provide different shorthands for different purposes. So happens that += is defined as such in Python.
4th Jul 2018, 10:37 AM
Hatsy Rei
Hatsy Rei - avatar