+ 30
[solved] Is there any difference between a += 2 and a = a+2
Amazing question by Varun a = [1,2,3] def func(a): a += [4] return a print(a==func(a)) b = [1,2,3] def func2(b): b = b + [4] return b print(b==func2(b)) Guess the output https://code.sololearn.com/cPC7ogbQRfHu/?ref=app
49 Réponses
+ 16
Namit Jain https://www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-a-b-is-not-always-a-a-b/
This will clear you doubts
+ 19
The operator += is implemented by dunder(magic method) __iadd__().
Each class may implement it, as it wants.
The class list implemented it as a simple append.
But an append keeps the id of the list.
Possible would have been an implementation in the sense of a = a + [b]
Here the variable a would have changed its identity.
So it was a choice and Guido decided the inplace variant.
Now we look at string.
String is immutable.
Since you cannot change it, you cant implement += operator in a useful way without creating a new string.
+ 10
Namit Jain
yeah each class can define += differently.
So a list simply appends with +=
while an immutable string creates a new instance.
In your question you had an integer where it makes no difference.
+ 9
Namit Jain In the 1st case you are modifying the list in place but in the 2nd case you are creating a new list.
Printing them clears the doubt.
https://code.sololearn.com/c9iM4MwDJraJ/?ref=app
+ 9
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Yes you are right with the id thing because it returns a new list which will have a new reference.
+ 8
Priyanshu Gupta RATIKANT PARIDA Subhajit Dey Michael David Gekoda Devesh Sony # ПёсБарбос Xojiakbar Kartik Ghatmal Akanksha Akki
I think you should read the whole question and the conversation....
You will know, they are a bit different in some cases...
see this code
https://code.sololearn.com/cPC7ogbQRfHu/?ref=app
+ 8
Devesh Sony I think now you read the conversation cuz you were saying that both are same and now you changed your statement
+ 6
Namit Jain Everything in python is treated as object. If you see list, string, int, float all data types are basically a class. In every class operator + and += is overloaded as per decision of python language developer. That is make a difference.
In some cases difference in id's is due to dynamic reference property of python.
https://www.codementor.io/@arch/variable-references-in-python-u9z8j2gk0
+ 5
Taran Grover Thank you!!
That's a good website...
Helped me a lot
+ 5
Xojiakbar No
+ 4
Namit Jain If you’re familiar with Python, you would have known Increment and Decrement operators ( both pre and post) are not allowed in it.
+ 4
Gaurav in some cases it is different
I am saying again
Check this article
https://www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-a-b-is-not-always-a-a-b/
+ 4
Namit Jain Yeah i have read the whole question and also conversation. And i agree will Taran Grover . It reacts differently for some cases.
+ 4
A=2
A+=2
(Outputs 4)
A+2
(Outputs 4)
几ㄖ 刀iffernc乇...😑
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 I still can't understand
Do you want to say that
a += 1 != a = a + 1
+ 3
Yess I know that Oma Falk 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Taran Grover
I was trying to relate it with other languages... I can't understand why the output is True and False in this code...
https://code.sololearn.com/cPC7ogbQRfHu/?ref=app
+ 3
Avinesh So, sir
It proves that:
a += 1 != a = a + 1
In some cases
+ 3
Namit Jain Run below code you will get behavior.
def test1(list1,list2):
print(id(list1))
list1 += list2
print(id(list1))
print(list1)
def test2(list1,list2):
print(id(list1))
list1 = list1 + list2
print(id(list1))
print(list1)
objects = [ [[1, 2],[3,4]], ["Great", "Saga"], [1,2], [1.23, 2.34] ]
for i in objects:
test1(i[0], i[1])
print("≠==========≠")
test2(i[0], i[1])
print("************")
Please don't compare with ++a and a++ as ++ operator is not overloaded in python. If you want to do that you can make your own user define function.