Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Immutable and Mutable objects in Python/Sagemath
Immutable and Mutable objects in python
Integers, floats, strings, booleans and tuples are immutable objects. list
, dict
and matrices (among other things) are mutable objects. In python, unlike many other programming languages (like C), assigning a variable to an object (ie var = 5) simply assigns the name var
to represent that object instead of copying it. For immutable objects, there is usually no confusion.
However, mutable objects like lists and matrices often just change the referenced object instead of copying. This can lead to some really unexpected behavior. Thus, assigning a list/matrix to a variable name just makes that name a reference to that list/matrix instead of actually copying the object.
To get around this, you should use the copy()
function to make a copy of the object that will not reference the original.