Change language

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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically