Python: Everything is an Object

Yes, that is right; everything is an object.

The beloved Python documentation spins it as follows…

Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects.’

Ok, so what does that mean? Akin to the varied objects surrounding you in life, these objects also have their own capability, limitations, and attributes. Once we understand them, we will better utilize the functionality that python provides.

id() and type()

These two [very neat] built-ins will kick us off into exploring python. Python has different categories of data that we refer to as types. When you use type() on a variable or value, it will return that category — the data type. Cool, huh?

(disclaimer: Ubuntu 14.04 LT, python3 (version 3.4.3) )
<class 'dict'>

>>> cities = ['Chicago', 'Rome', 'San Francisco']
>>> type(cities)
<class 'list'>

>>> twins = ('tweedle dee', 'tweedle dum')
>>> type(twins)
<class 'tuple'>

>>> type(1)
<class 'int'>

>>> a = "Naomi"
>>> type(a)
<class 'str'>

id() is a whole different story. Let’s take the same variables from above, and call id() on them…

>>> id(my_favs)
140023825699400
>>> id(cities)
140023818359880
>>> id(twins)
140023786219656
>>> id(1)
10055552
>>> id(a)
140023818447424

What is that number? It’s a unique, constant integer assigned to the object that will not be assigned to another object so as long as it exists. This number is akin to the address in memory that we find in C programming.

Okay, cool deal, but what happens if we play around with these variables?

>>> cities += ['New York City']
>>> print(cities)
['Chicago', 'Rome', 'San Francisco', 'New York City']
>>> id(cities)
140023818359880

Did you notice that their id numbers didn’t change? Wait…what?

Mutable Objects.

Python data types are either mutable or immutable (yes, we will get to the later momentarily). This means that they are able to be modified and changed (mutable) or not. Lists, dictionaries, sets, and user-defined classes are all examples of mutable data types. Looking at the prior example, you’ll notice that we just changed the existing list which was associated with the id number 140023818359880.

What if we were to assign that same list, cities, to a new_cities list?

>>> new_cities = cities
>>> print(new_cities)
['Chicago', 'Rome', 'San Francisco', 'New York City']
>>> id(new_cities)
140023818359880

You guessed it! (at least, I hope you did…) Same id! It’s because new_cities and cities lists are each referencing the same place in memory where our changeable, appendable list is stored.

Immutable Objects.

Immutable objects include integers, floats, strings, boolean, tuples, decimal, and complex. These data types cannot be changed after their creation in memory. Look at an example…

>>> a = "Naomi"
>>> id(a)
140023818447424
>>> a+= " Dawn"
>>> print(a)
Naomi Dawn
>>> id(a)
140023786220272

Although we similarly added to this string as we had done with the list above, we have a new id; this is because when we concatenated “Naomi” and “Dawn” we created a new combined string in memory after referencing the old one.

>>> a = 1
>>> id(a)
10055552
>>> b = a
>>> id(b)
10055552
>>> a+= 1
>>> id(a)
10055584
>>> id(b)
10055552
>>>

Here is an integer example:

>>> a = 1
>>> id(a)
10055552
>>> b = a
>>> id(b)
10055552
>>> a+= 1
>>> id(a)
10055584
>>> id(b)
10055552

When the value of ‘a’ was set to the variable ‘b’, they each had the same id number, however once ‘a’ was incremented it had a new id. ‘b’ stayed the same, though because it continued to reference the place in memory to which it had originally been assigned.

Why does any of this difference matter?

As noted above, if the manipulation of immutable types necessitates a new version, it will have to create more space for it. If we want to modify a data structure or variable multiple times, it’d be a much better use to use a mutable type.

Inversely, if we don’t want our variables to be as easily changed or effected, immutable provides a great option.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.