Notes on Python Objects

Introduction

This article will walk you through the basics of Python objects; we will discuss mutable and immutable objects, and the inbuilt functions id() and type().
Before we dive in, What is Object Oriented Programming?
Object Oriented Programming (OOP) is an approach or a paradigm of programming revolving around the concept of classes and objects. It also implements four major concepts — Inheritance, Polymorphism, Abstraction, and Encapsulation.

Image source: DataCamp

Everything(data) we work with in Python is an object of a class. Classes are blueprints for creating objects. In OOP, we define a structure in what is known as a class and then we use that class to create objects. When you create data in Python, you are creating an object which is an instance of a specific class. The class defines how an object can interact with other parts of a program.
Asides from the classes and objects you can create when working with Python, there are built-in classes that have been pre-defined in Python.

Image source: Quora

ID and Type Functions

The built-in function id() gives us the ability to check the unique identifier of an object. The unique identifier points to a location in the memory which is an object. When an object is passed through the id() function as an argument, it returns the memory address in the CPython implementation. This function can also be used to determine whether or not two variables point to the same object using the equality operator (==).

The built-in function type() takes an object as its argument and returns the type of that object. The type() function can also be used to initialize classes or existing classes with attributes dynamically by taking three arguments.

From the image below, we can see that the type of the variable a is a string and it is an object of the built-in class str (string).

Mutable Objects

Mutable objects are objects that are modifiable. This means that the content and state of a variable that is mutable can be changed or mutated at any time by another method. Examples of mutable objects are lists, sets, dictionaries, and byte arrays.

From the image above, we can see that the integer at index 0 of the mutable_obj list could be modified. This is because a list is a mutable object.

Immutable Objects

Immutable means non-modifiable. This means they cannot be changed after they are created. With immutable objects, no matter how many times a function is called with the same variable, the output will always be the same. Examples of immutable objects are all primary data types like integers, strings, complex, tuples, bool, float, frozen sets, and bytes.

From the image above, we can see that the immutable_obj string returned an error on an attempt to be modified. This is because a string is an immutable object.

Note: Although tuples and frozen sets are immutable, they can contain mutable objects.

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

Mutable and immutable objects are treated differently in Python because of their characteristics. Mutable objects are slower to access than immutable objects. Mutable objects are also more expensive to change than immutable objects because changing them requires the creation of copies which requires more memory. Mutable objects are recommended when there is a need to change the size or content of the object, while immutable objects are used when you want to guarantee that the value of an object will always stay the same. Immutable objects are also easier to debug.

How arguments are passed to functions and what that implies for mutable and immutable objects

Python passes arguments to functions by assignment. This is similar to passing arguments by reference. The passed arguments are assigned to the function’s local variables. However, simply because the called function has access to the address of the caller’s arguments does not mean that it can change any of the objects those arguments refer to. Only objects that are mutable and referenced by provided parameters can be modified. On the mutable objects specified by the parameters, aliasing is possible.

Passing by reference means that the caller’s given argument will be the same as the called function’s parameter. The called function’s parameter will be a copy of the caller’s supplied argument when pass by value is used.

NSMALLPOSINTS and NSMALLNEGINTS

The integers -5 to 256 are NSMALLNEGINTS to NSMALLPOSINTS. This collection of 262 integers has been pre-allocated, and all references to them are aliases. This is due to the fact that they are the most frequently used integers, and it is reasonable to cache them.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cumi Oyemike

Cumi Oyemike

Cloud/DevOps | Backend development | Bookss | Good music | Debates