[solved] Is there any difference between a += 2 and a = a+2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

9th Jun 2020, 5:54 PM
Namit Jain
Namit Jain - avatar
49 Answers
+ 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.
9th Jun 2020, 6:26 PM
Oma Falk
Oma Falk - avatar
+ 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.
9th Jun 2020, 6:06 PM
Oma Falk
Oma Falk - avatar
+ 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
9th Jun 2020, 6:17 PM
Avinesh
Avinesh - avatar
+ 9
Thank you Avinesh sir!! Thank you Oma Falk !!🙏
9th Jun 2020, 6:25 PM
Namit Jain
Namit Jain - avatar
+ 9
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Yes you are right with the id thing because it returns a new list which will have a new reference.
9th Jun 2020, 6:28 PM
Avinesh
Avinesh - avatar
+ 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
10th Jun 2020, 2:06 PM
Namit Jain
Namit Jain - avatar
+ 8
Devesh Sony I think now you read the conversation cuz you were saying that both are same and now you changed your statement
10th Jun 2020, 3:36 PM
Namit Jain
Namit Jain - avatar
+ 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
10th Jun 2020, 5:49 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 5
Taran Grover Thank you!! That's a good website... Helped me a lot
9th Jun 2020, 6:21 PM
Namit Jain
Namit Jain - avatar
+ 5
10th Jun 2020, 3:19 PM
Namit Jain
Namit Jain - avatar
+ 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.
9th Jun 2020, 6:14 PM
Taran Grover
Taran Grover - avatar
+ 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/
9th Jun 2020, 6:50 PM
Taran Grover
Taran Grover - avatar
+ 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.
10th Jun 2020, 3:30 PM
Ghost09
Ghost09 - avatar
+ 4
Xojiakbar I think you shud do the python course again Here the value of a and b is None because we cannot use assignment operator along with append and insert
10th Jun 2020, 4:12 PM
Namit Jain
Namit Jain - avatar
+ 4
A=2 A+=2 (Outputs 4) A+2 (Outputs 4) 几ㄖ 刀iffernc乇...😑
11th Jun 2020, 4:16 PM
マッドキング♣️✨♣️ [MadKing]
マッドキング♣️✨♣️ [MadKing] - avatar
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 I still can't understand Do you want to say that a += 1 != a = a + 1
9th Jun 2020, 6:08 PM
Namit Jain
Namit Jain - avatar
+ 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
9th Jun 2020, 6:17 PM
Namit Jain
Namit Jain - avatar
+ 3
Avinesh So, sir It proves that: a += 1 != a = a + 1 In some cases
9th Jun 2020, 6:19 PM
Namit Jain
Namit Jain - avatar
+ 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.
10th Jun 2020, 5:41 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar