Normal operators do the simple assigning job. On other hand, Inplace operators behave similar to normal operators except that they act in a different manner in case of mutable and Immutable targets.
- The _add_ method, does simple addition, takes two arguments, returns the sum and stores it in other variable without modifying any of the argument.
- On the other hand, _iadd_ method also takes two arguments, but it makes in-place change in 1st argument passed by storing the sum in it. As object mutation is needed in this process, immutable targets such as numbers, strings and tuples, shouldn’t have _iadd_ method.
- Normal operator’s “add()” method, implements “a+b” and stores the result in the mentioned variable.
- Inplace operator’s “iadd()” method, implements “a+=b” if it exists (i.e in case of immutable targets, it doesn’t exist) and changes the value of passed argument. But if not, “a+b” is implemented.
In both the cases assignment is required to do to store the value.
Case 1 : Immutable Targets.
In Immutable targets, such as numbers, strings and tuples. Inplace operator behave same as normal operators, i.e only assignment takes place, no modification is taken place in the passed arguments.
Value after adding using normal operator : 11 Value after adding using Inplace operator : 11 Value of first argument using normal operator : 5 Value of first argument using Inplace operator : 5
Case 2 : Mutable Targets
The behaviour of Inplace operators in mutable targets, such as list and dictionaries, is different from normal operators.The updation and assignment both are carried out in case of mutable targets.
Value after adding using normal operator : [1, 2, 4, 5, 1, 2, 3] Value of first argument using normal operator : [1, 2, 4, 5] Value after adding using Inplace operator : [1, 2, 4, 5, 1, 2, 3] Value of first argument using Inplace operator : [1, 2, 4, 5, 1, 2, 3]
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...)
- Inplace Operators in Python | Set 2 (ixor(), iand(), ipow(),…)
- Python program to represent floating number as hexadecimal by IEEE 754 standard
- Division Operators in Python
- Basic Operators in Python
- Chaining comparison operators in Python
- Increment and Decrement Operators in Python
- Python Membership and Identity Operators | in, not in, is, is not
- Python | Splitting operators in String
- Logical Operators on String in Python
- Python | Solve given list containing numbers and arithmetic operators
- G-Fact 19 (Logical and Bitwise Not Operators on Boolean)
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script