Objectively Python

Benjamin Keener
4 min readJan 16, 2020

Being a higher-level language, Python seems to use a lot of voodoo when it comes to, well, everything. One of the keys to Python’s ease and flexibility is due to the fact that everything in Python is an object. Variables, classes, functions, everything. In this article, I’ll touch on a few different types of object in Python and the differences between them.

Types are the building block of all objects in Python. Every object has a type which can be shared between multiple objects. What cannot be shared between objects is their ID. Each python object has its own id, which is necessary for it to be differentiated from other objects. Enough of the mumbo-jumbo, let’s look at an example.

Initializing an integer, a

Here, we make a new variable (which is an object) and give it the integer value of 3. we can use Python’s type() and id() to get relevant information on this object.

Showing a’s type and id

So, now we can see a's unique number and it’s data type. This carries over to any object in Python, not just ints and not just variables.

Now, let’s talk about mutable vs. immutable object types and let’s start with ints. Ints are immutable, but how? We can change an integer’s value, right? Well, yes, but also no.

Here we’ve made a new variable, b, an int. Now, if we change b, what happens?

It looks like we can change it, so how come it’s immutable? Well, the secret shows itself when we check the id of b.

It differs from the original id that b had. This is because we haven’t actually change the value, we’ve just told b to point to another integer. Very strange, but it’s how Python handles different types of data.

Let’s look at a mutable type for contrast, the list.

Here we’ve created the list, given it some values, and checked its id. Let’s change something about the list and see what happens.

Sure enough, the id is the same as it was originally. That’s because we’re actually mutating the data that l is pointing at, instead of just telling it to look somewhere new.

So how does this affect us when we, say, pass data to a function? Well, in Python, data gets passed a reference to the data. In the case of an immutable object, if we want to change the value of the argument passed to us, we aren’t actually changing the data, just having our argument variable point somewhere else. On the other hand, mutable objects can be changed in a function, so when we pass something like a list to a function, we can actually manipulate the data at that spot, which can cause some pretty confusing behavior.

Let’s look at this simple program:

def change1(arg):
arg += 1
def change2(arg):
arg += [4]
l = [1, 2, 3]
id(l)
Output: 140670892100808
a = 47
id(a)
Output: 10106560
change1(a)
print(a)
Output: 47
id(a)
Output: 10106560
change2(l)
print(l)
Output: [1, 2, 3, 4]
id(l)
Output: 140670892100808

As we can see from this example, our int variable does not change from the function and therefore its id stays the same. Our list, however, is affected, but the id does not change because the list is mutable and we changed the actual data.

These properties are true for all mutable and immutable datatypes in Python and I hope I’ve encouraged you to dig a little deeper into Python’s funky inner workings.

--

--