Objects in Python

John Cook
4 min readMay 30, 2019

--

Today I am going to talk about objects in python, specifically their ids and types, the differences between mutable and immutable objects, why this matters, and how arguments are passed to functions in this manner. While there are many uses to object oriented programming I will be keeping it simple, I will only be going over the topics stated above to save time.

Let us start off with explaining the id inbuilt function, id is an inbuilt function that gets the unique id of an object, it is kinda like its address in memory but not quite. If I enter the below code into the python 3 terminal I will get the following:

>>> a = 1
>>> id (a)
10910400
>>>

where id gives the id of the object a, this is 10910400 on my machine at the time of writing, this number can vary, but the important thing to know is that it is unique. This uniqueness lets us tell if two objects are the same, for example we can type the following:

>>> a = 1
>>> id (a)
10910400
>>> b = a
>>> id (b)
10910400
>>>

and notice how b has the same id as a, this is because one is aliased to another, or b is aliased to a in this case, this essentially means that a and b are the same thing just different names for it. The type method lets us know what type an object is, it could be an int or a float or a string, sometimes it is necessary to know what kind of type we are dealing with. I will only be covering how type acts when passed an object but know that it can be used for more. For example the following code can be used to see the usefulness of type:

>>> a = (1)
>>> type (a)
<class 'int'>
>>> b = (1, 2)
>>> type (b)
<class 'tuple'>

as you can see the type of an object can be useful to know, type allows us to check a type and make sure that the input is correct.

if type(a) is not int:
raise TypeError("wrong type")

The above sudo code example is particularly useful when dealing with user generated input, it allows us to make sure that the user enters an integer and not a string.

Lets move on to mutable objects. Mutable objects are like water, they can change their state, just like water can be a liquid or a gas or a solid, mutable objects can change their state or contents. Mutable objects are list, dicts, sets, and generally custom classes. Below I will give an example of a mutable list:

>>> print(fruit)
['apple', 'banana', 'orange']
>>> fruit[-1] = "pineapple"
>>> print(fruit)
['apple', 'banana', 'pineapple']
>>> fruit = ["apple", "banana", "orange"]
>>> id (fruit)
140428709766280
>>> fruit[-1] = "pineapple"
>>> id (fruit)
140428709766280

notice how the contents of the list can change but the list id stays the same, so while the contents of fruit change the list fruit will not change.

Now if mutable objects are like water then immutable objects are like rocks, they don’t change much. If we try to change an immutable object such as a(n): int, float, bool, string, unicode, tuple (tuple is a special case as it itself is immutable but its contents can be mutable) then we get a trace back with the appropriate error message. I can however change what the immutable type points to, so I can effectively change the values, as shown below:

>>> a = 0
>>> a += 3.14
>>> print (a)
3.14

This is an important distinction to make, I can always change what the value points to but I can not change the value of 3.14 because it will always be equal to 3.14.

Now we can explain why this matters. If ints where mutable then the following code would not work:

>>> a = 2
>>> b = 3
>>> print (a + b)
5
>>> print (a)
2
>>> print (b)
3

if ints where mutable then a would = 5 and so would b. If list where not mutable then it would kind of defeat the purpose of list. Python will not allow an immutable object to be changed in the same way as a mutable object, this allows for different functionality. This difference in functionality allows mutable objects to be added or subtracted to, in addition to other functions, while preserving the stability of immutable objects.

I am sure by now that you are wondering why this matters, it matters because python passes all function arguments as assignments, meaning it passes a value by reference. This means for mutable objects you can pass to a function and use a method in that function and the outer scope variable will change, but if you assign the outer scope variable to an inner scope variable then it will remain the same. Below is some sudo code to help illustrate the idea.

def foo(my_list):
my_list.append('hi')
outer_scope = ['hello', 'howdy']
foo(outer_scope)
print(outer_scope)
['hello', 'howdy', 'hi']

Notice how the list is appended. If we assign a the passed variable to an inner scope and call our method on that it will not change the outer scope.

def foo(my_list):
inner_scope = my_list
inner_scope.append('hi')
outer_scope = ['hello', 'howdy']
foo(outer_scope)
print(outer_scope)
['hello', 'howdy']

So if we want to change the value of a immutable type we have to change its reference we can simply do this by assigning it a new value, note we can not change the value that is stored in the immutable type, but we can change what the immutable type points to. I hope this has helped explain why object type matters and explain why we should care.

--

--

No responses yet