Python3: Mutable, Immutable... everything is object!
Photo from Goodmetaphysics

Python3: Mutable, Immutable... everything is object!

Introduction and TLDR:

Pretty much everything in Python is an object. Keywords like for is not an object. All the ints, strings, and data structures are objects. These objects are mutable or immutable, which means it can change or not, respectively. Every variable or name refers to an object instance.

ID and Type

Every object has an id associated with it, an integer that defines the unique object. Usually this number corresponds to the object's memory location. In the python interpreter, you can type something like a = 3 and then type id(a) to get a's id. Type is the data structure. If a an int? is it a string? type type(a) in the interpreter to find out! Here is another resource and a good reference.

An object's type is set at runtime and immutable. The object's state is mutable, maybe... It depends. The keyword 'is', compares id and == compares values.

Mutable objects

This means they can change. Below is a table. As you can see, you can modify lists, sets, and dictionaries. When you have a = 1 and then type a = 3, you are not changing the integer a points or references to. You are simply creating a new object and changing what a is referencing. So you created two objects, 1 and 3, and simply changed which one a looks at.

Immutable objects

This means you can't change them. Kinda. Python has it's nuances. Try the following example: If I had A = "watermelon", get the id of that via id(A), and then did A += "a". Now A is "watermelona" but the id(A) is completely different. This "watermelona" is a new object! But here is the mind blown part. If we did A = A + "a", we get "watermelonaa" now but the id of the old and this one matches. WUT DA EFF DAVID BLAINE?!?!

Why does that happen?? Assignment is not mutation in Python.

Basically the right side is computed first then left side assignment is done.

How arguments are passed to functions and what does that imply for mutable and immutable objects?

Immutable objects are passed by reference since they "cannot" be changed. But since mutable objects can change, another copy is passed to them; this is horrible for memory. We can call by reference or pass by value.

Why does it matter and how differently does Python treat mutable and immutable objects

Python handles objects weirdly. There is caching to worry about also.


MORE SICK TIPS AND INFO!!!

  • Assignment in Python never copies values, it only copies references. Everything is pointers! Variables are assigned to objects. Objects are created before the assignment. In an example like y = x + 1, the right side is evaluated first and a new object is initialized or retrieved and then the y is assigned to that. The left side binds to the object after the right side is finished.
  • Think of variables to objects as labels on a box instead of putting variables in a box.
  • Simple but elegant pythonic code
for index, value in enumerate(numbers):
    numbers[index] = value**2
  • Passing a list as an argument only passes a reference, not a copy. Python references are not quite pointers but they do share characteristics.
  • Modifiers are functions that take in a list as an argument and change them while executing. Side effects are what we call those changes. pure functions do not have any side effects.
  • Pure functions are usually less error prone and faster to develop and some languages only allow pure functions.
  • (+=) will change the ID of a string but not a list. Wut is with this inverse inconsistancy?!?=! Eg. a = a+[5] will give a diff id for a, before and after. The id for a in a += [5] is the same before and after. But if we did this for a string the results are opposite.
  • In C and on my machine, an int is only 4 bytes where as in python, an int is 24 bytes. Python is easy, simple, great, and convenient but it is trash regarding speed and memory (sorry).
  • Accessing immutable objects are faster. It is also nice due to the const nature of it.
  • There are exceptions to immutability. There is a lot of info that can be covered here so please just Google it.
  • Python can be a compiled and interpretive language.
  • Class is a blueprint, object is the houses built from it, and instance is one specific house.
  • Assignment is when you use =. == is comparing values and checking if they are the same. Using is will check if the two objects are identical.
  • Alias is a synonym for 2 variables that point (reference) to the same object.
  • Strings are immutable in python so it optimizes resources by referring to the same string object. This is not the case for lists. This is not the case if there are spaces in the string. Python has many exceptions cause it is too noob friendly.
  • Python caches strings with no spaces. It also caches -5 to 256.
  • User defined classes are mutable.
  • You think you know, but you don't

To view or add a comment, sign in

More articles by Bryan Leung

Others also viewed

Explore content categories