Python a + = b and a = a + b are different

Some sites say that the operator ʻa + = b is the same as ʻa = a + b, but it is not exactly the same for mutable objects. Be careful if you have variables that point to the same object. (Especially for those who use NumPy!)

Overview

In a nutshell, there are the following differences. For mutable objects,

--In ʻa + = b, the object pointed to by ʻa does not change before and after the assignment. --When ʻa = a + b, the object pointed to by ʻa changes before and after the assignment.

However, for immutable objects, it changes in both cases (@shiracamus's comment has an example).

Experiment

Let's experiment with adding [3] to [1, 2] to create [1, 2, 3]. y refers to the object of x before assignment. You can get the ID of an object with the built-in function ʻid (x) `.

a += b

>>> x = y = [1, 2]  #y is the same object as x before assignment
>>>
>>> id(x)
4397797440
>>> id(y)
4397797440
>>>
>>> x += [3]
>>> 
>>> x
[1, 2, 3]
>>> y  #Also added to y
[1, 2, 3]
>>>
>>> id(x)  #Same object as before assignment
4397797440
>>> id(y)
4397797440

a = a + b

>>> x = y = [1, 2]  #y is the same object as x before assignment
>>>
>>> id(x)
4397797440
>>> id(y)
4397797440
>>>
>>> x = x + [3]
>>> 
>>> x
[1, 2, 3]
>>> y  #Not added to y
[1, 2]
>>>
>>> id(x)  #Point to an object different from the one before the assignment
4395937472
>>> id(y)
4397797440

Recommended Posts

Python a + = b and a = a + b are different
[Python] return A [or / and] B
[Python3] "A // B" and "math.floor (A / B)" are not always the same! ??
Python3> round (a --b, 7)
python memo-"if not A and B" was "if (not A) and B"
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
ABC127 A, B, C Explanation (python)
Are macOS and Linux completely different?
numpy's matrix and mat are different
Different from the import type of python. from A import B meaning
[Python] What are @classmethods and decorators?
a () and a.__ call__ () are not equivalent
Create code that outputs "A and pretending B" in python
ABC128 A, B, C commentary (python)
ABC126 A, B, C Explanation (python)
A python regular expression, str and unicode that are sober and addictive
Solve ABC175 A, B, C in Python
Modules and packages in Python are "namespaces"
A memo with Python2.7 and Python3 on CentOS
Connect a lot of Python or and and
python> keyword arguments> hoge (** {'a': 1,'b': 2,'c': 3})
yum and apt update / upgrade are different
Solve ABC165 A, B, D in Python
A story about Python pop and append
Python open and io.open are the same
Organize python modules and packages in a mess
Building a python environment with virtualenv and direnv
The answer of "1/2" is different between python2 and 3
A nice nimporter that connects nim and python
How python classes and magic methods are working.
ffmpeg-Build a python environment and split the video
I wrote a class in Python3 and Java
Why I'm a Java shop and start Python
Create a web map using Python and GDAL
Launch a web server with Python and Flask
What are you comparing with Python is and ==?
Let's write a Python program and run it
Create a Mac app using py2app and Python3! !!
A story about modifying Python and adding functions
[Python] A rough understanding of iterators, iterators, and generators
A discussion of the strengths and weaknesses of Python
What are python tuples and * args after all?
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
[python] Compress and decompress
A * algorithm (Python edition)
Python and numpy tips
[Python] pip and wheel
[Python] Take a screenshot
Python: Creating a virtual environment (venv), starting and stopping
Try creating a compressed file using Python and zlib
Create a Python module
Batch design and python
Python iterators and generators
Building a Docker working environment for R and Python
Build a python virtual environment with virtualenv and virtualenvwrapper
A python lambda expression ...
Python packages and modules
Vue-Cli and Python integration
It would be wise to write like boolean and "A" or "B" [Python] [But]
Python classes are slow
Ruby, Python and map