Everything is an Object!

Gabriel Vazquez
4 min readMay 25, 2021

Assuming you’re already somewhat familiar with the Python programming language I’m sure you’ve heard it referred to as “object-oriented”, today we will be taking a deep dive into what exactly that means. To simplify let’s break it down into a series of topics: what even is an “object”? What are their properties? What makes this language different from others? Lets get started!

What even is an object?

To understand what object-oriented means, let’s first focus on objects themselves, which can be an elusive concept to grasp. In python any collection of data can be an object, and the behavior of this data will define what kind of object that is. The behavior of this data is dependent on what attributes it possesses. For example, variables are a type of object that can have data assigned to them for storage which allows us to easily keep track of values that we may need later. The attribute in this case is the ability to store data to memory under an alias (variable name). This type of object can also be passed as an argument to our second type of object: functions. Functions are a type of object that can perform operations. They possess a unique attribute which is that they themselves can be manipulated as data (just like variables!) but they can also be called upon to perform the manipulation of data. To summarize: objects are python’s method to contain and organize data depending on the characteristics of the data itself and what the intended purpose of the data is.

How does Python know different objects from one another?

Python has a clever way of figuring out what kind of object it’s dealing with. There are two main distinctions in object type: mutable and immutable. Mutable objects are those whose content you can change without directly affect their identity. For example a list is a mutable type of object where you can call a function to append something to the end of the list, netting us a new, different object (our new list with an added value at the end) but it is still the same object type. Almost every object type in python is of the mutable type. However, immutable objects such as floats and ints cannot have their inherent value changed. For example say we have an int variable ‘a’ where a = 0. If we try to do a += 1, our new value will be 1. However the value of 0 itself wasn’t changed but rather where the value of where ‘a’ points to in memory. Python gives us two handy functions with which we can verify the values of these objects: the type() and id() functions. The type function will return what type of object it is and the id function will return the unique address tied to it’s value. By using these we can verify if changing the value of a variable changes it’s type and unique identity or not.

Example of using id() and type() on immutable objects
Example of using id() on mutable objects

Passing as arguments

This mutable vs immutable distinction is one that differentiates python from other programming languages and can be the cause of great confusion. Hopefully in the previous paragraph we cleared it up a bit. Let’s now examine what effects this has on the ability to assign variables and pass them to functions. Using an immutable object ensures that the information within keeps it’s identity even beyond the scope of the function calling it.

Since functions themselves are objects python is equipped to handle passing them as arguments as well. The encompassing function is known as a wrapper function or decorator. These allow you to modify the behavior of a function without permanently modifying them. This ability grants python an added level of flexibility when it comes to modifying output with minimal code intervention. This is one of the reasons python is considered such a user-friendly programming language and is regarded as many as a great place to start for novice coders.

Here’s an example of passing functions as arguments

In the previous example we are defining our caps function to take a string as an argument, but later we assign this function to a variable. This causes our program to not call the function itself but rather it takes the function object referenced to by caps and assigns it a new name that points to it, i.e. yelling.

And here’s the produced output

Hopefully this humble blog helped clarify a bit on the internal mechanisms of what makes python such a powerful and versatile programming tool!

--

--