Inplace vs. Standard Operators in Python

Regular operators perform a simple assignment task. On the other hand, Inplace operators behave similarly to regular operators, except they act differently for mutable and immutable targets.

  • The method _add_ does a simple addition, takes two arguments, returns the sum, and stores it in another variable without changing any argument.
  • On the other hand, the _iadd_ method also accepts two arguments, but it makes changes to place in the 1st argument passed by storing the sum in it. Since this process requires mutation of the object, immutable targets such as numbers, strings, and tuples should not have a _iadd_ method.
  • Normal add () statement implements “ a + b ” and stores the result in said variable.
  • Insert operator method “iadd ()” , implements “ a + = b “, if it exists (i.e. E. In the case of immutable targets, it does not exist), and changes the value of the argument passed. But if not, then “a + b” is implemented .
  • In both cases, an assignment is required to store the value.

Case 1 : Fixed targets.
Fixed targets such as numbers, strings, and tuples. In-place operators behave the same as regular operators, i.e., only assignment is performed, no changes are made to the arguments passed.

# Python- code to show the difference between
# Inplace and Normal statements for immutable purposes

 
# import statement to handle statement operations

import operator

 
# Initializing values ​​

x = 5

y = 6

a = 5

b = 6

 
# using add () to add passed arguments

z = operator.add (a, b)

 
# using iadd () to add passed arguments

p = operator.iadd (x , y)

 
# print the changed value

print ( "Value after adding using normal operator:" , end = "")

print (z)

 
# printing modified value

print ( "Value after adding using Inplace operator:" , end = "")

print (p)

 
# print the value of the first argument
# the value does not change

print ( " Value of first argument using normal operator: " , end = " " )

print (a)

  
# print the value of the first argument
# the value does not change

print ( "Value of first argument using Inplace operator:" , end = "")

print (x)

Output:

 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
Behavior of Inplace operators for mutable targets, such as lists and dictionaries are different from ordinary operators.  Updates and assignments are performed in the case of mutable targets.

# Python code to show the difference between
# Inplace and Normal statements for editable purposes

 
# import statement to handle operator operations

import operator

 
# Initializing list

a = [ 1 , 2 , 4 , 5 ]

  
# using add () to add passed arguments

z = operator.add (a, [ 1 , 2 , 3 ])

 
# printing the changed value

print ( " Value after adding using normal operator: " , end = " ")

print (z)

 
# print the value of the first argument
# no value changes

print ( "Value of first argument using normal operator: " , end = " ")

print (a)

 
# using iadd () to add passed arguments
# does + = [1 , 2, 3]

p = operator.iadd (a, [ 1 , 2 , 3 ])

 
# print the changed value

print (< / code> "Value after adding using Inplace operator:" , end = "")

print (p)

 
# print the value of the first argument
# value changed

print ( " Value of first argument using Inplace operator: " , end = "")

print (a)

Output:

 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 courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.