Python Basics: Everything is an object

Mutable vs Immutable Objects

Erika Caoili
Analytics Vidhya
5 min readJan 16, 2020

--

Photo by Oscar Nilsson on Unsplash

Learning Objectives for this post:

A brief introduction to classes, objects, and everything is an object.

What are an object’s id and type?

What are mutable objects and immutable objects?

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

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

Introduction (Object-Oriented vs. Procedure-Oriented)

Python utilizes the Object Oriented Programming (OOP) approach to writing programs. Procedure Oriented Programming utilizes functions while OOP uses objects. In OOP, you write classes that represent real-world things and situations. You can think of a class as a blueprint for creating objects. Thus, you create objects based on classes. Objects are a collection of data and methods that act based on the data they hold. You can think of the data as variables and the methods create the functionality. They are both packaged together to create an object with the class specifying what it will do.

For example, imagine a class being a sketch or a prototype of a car. The class contains the specifications for the parts to build the car and the functionality of it. Based on these attributes, you can make a general car. The car is the object. You can also, create individual objects from a class and you can give each object any unique traits you want. For instance, each car is automatically equipped with the general behavior, but you can customize its color, make, and model

The class creates the basic car on top. The individual objects are cars with specified unique traits written by the programmer.

Everything is an object or “first class everything” are common phrases to describe Python, but why?

In Python code, all the objects represent data or its relation between objects that hold other data.

“All of Python’s objects were based on a common C data structure that was used everywhere in the interpreter. Variables, lists, functions, and everything else just used variations of this one data structure — it just didn’t matter if the structure happened to represent a simple object such as an integer or something more complicated such as a class.” — Guido van Rossum, creator of Python

Objects, Values, and Types

Identity (id) =

Once created, an object’s identity never changes. The id() function returns a unique set of integers for the specified object and in Python, all objects have their own unique id. Note, objects that have a constant unique ids such as integers from -5 to 256 remain the same. You can think of the id as the object’s memory address.

id(0)          #10105056a = "hello"
id(a) #140296298136888

Type

Type defines the possible values and operations an object can have. Type outlines specific attributes that are unchangeable. Back to the car example, car attributes that would remain the same are its structure i.e. the car has a steering wheel, four wheels, an engine etc.

It returns the type of an object. The type() function has two different forms:

type(object)                  # a single object parameter 
type("Hello") # <class 'str'>
type(4) # <class 'int'>
type(name, bases, dict) #class name, a tuple, and dict

You can also use the isinstance() function to see the type of data value.

Values in Objects — Mutable vs Immutable

Mutable objects have values that allow change. Unlike immutable objects, whose value does not allow for change once they are created. With our car example, the objects that were immutable had the basic structure of the car itself. Each car had four wheels, four doors, an engine etc. However, the objects that became mutable were the make, model, and color of the car. So, the mutability of an object is determined by its type to represent what kind of value it is.

* Examples of Mutable objects = list, set, dict, and a byte array

* Examples of Immutable objects = number (int, float, complex), string, tuple, frozen set, and bytes

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

Immutable types allow for objects to have static specifications. These types have attributes that remain the same and unchanging. However, mutable types in Python creates a dynamic approach to writing your program. It allows you to determine functionality based on what you want.

  • Mutable objects (dict) can change in value, but the object remains the same.
  • Immutable objects will create a new object based on the changed value.
  • Mutable objects have easier access, thus making adjustments more useful.
  • When you want something to remain unchangeable, you can rely on immutable objects because its value will not change after you modify the program.

How are arguments passed to functions?

Mutable and immutable objects are passed in functions whether its an int, tuples, or lists. It’s important to remember that arguments need validation to verify that the arguments passed to the function meet our criteria.

When you pass a mutable type in a function, you understand that the value of the object will change.

def change(a)
a[1] = 24
l = ["c", "b", "a"
change(l)
print(l) # output: ["c", 24, "a"]

In immutable objects, the value of the object will remain the same. How does it work? In Python, you pass the immutable i to the function and it adds 1 to that object. Since you can’t change the value of the immutable object, Python will create a new object for that value. Thus, i will remain the same after executing the function. To change the value of i, add a return statement inside the function or access the value of the immutable value to modify it.

def increment(num)
num += 1
i = 1
increment(i)
print(i) # output of a is 1 because a = 1

Conclusion

Everything in Python is an object and its value is determined if the type is mutable (changeable) or immutable (unchangeable). It demonstrates how Python is an object-oriented language that has a dynamic approach to programming. Unlike procedure based programming that relies on a more static approach.

Resources

Object Oriented Programming

--

--