Everything is object in Python

cedouiri
4 min readJan 17, 2020

--

Functions in Python are first-class objects

Python Objects

While the class is the blueprint, an instance is a copy of the class with actual values, literally an object belonging to a specific class. It’s not an idea anymore; it’s an actual animal, like a dog named Roger who’s eight years old.

Put another way, a class is like a form or questionnaire. It defines the needed information. After you fill out the form, your specific copy is an instance of the class; it contains actual information relevant to you.

You can fill out multiple copies to create many different instances, but without the form as a guide, you would be lost, not knowing what information is required. Thus, before you can create individual instances of an object, we must first specify what is needed by defining a class.

id() function in Python

id() is an inbuilt function in Python. As we can see the function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during the lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

type() Method and Function

type() method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes. Two different types of arguments can be passed to type() function, single and three arguments. If the single argument type(obj) is passed, it returns the type of given object.

Mutable and Immutable objects

Simple put, a mutable object can be changed after it is created, and an immutable object can’t. Objects of built-in types like (int, float, bool, str, tuple, unicode) are immutable. Objects of built-in types like (list, set, dict) are mutable.

let's create an example:

a = 10
b = y

We are creating an object of type int. identifiers a and b points to the same object.

id(a) == id(b)
id(b) == id(10)

if we do a simple operation.

a = a + 1

Now:

id(a) != id(b)
id(a) != id(10)

The object in which a was tagged is changed. object 10 was never modified. Immutable objects doesn’t allow modification after creation

In the case of mutable objects

l = list([1, 2, 3])n = l

We are creating an object of type list. identifiers l and l tagged to the same list object, which is a collection of 3 immutable int objects.

id(l) == id(n)

Now poping an item from list object does change the object,

l.pop()

object id will not be changed

id(l) == id(n)

l and n will be pointing to the same list object after the modification. The list object will now contain [1, 2].

So what have we seen so far from the above examples?

  • python handles mutable and immutable objects differently.
  • Immutable is quicker to access than mutable objects.
  • Mutable objects are great to use when you need to change the size of the object, example list, dict, etc.. Immutables are used when you need to ensure that the object you made will always stay the same.
  • Immutable objects are fundamentally expensive to “change” because doing so involves creating a copy. Changing mutable objects is cheap.

Exceptions in immutability..

Not all of the immutable objects are actually immutable. Confused? Let me explain.

As discussed earlier, Python containers liked tuples are immutable. That means the value of a tuple can't be changed after it is created. But the "value" of a tuple is, in fact, a sequence of names with unchangeable bindings to objects. The key thing to note is that the bindings are unchangeable, not the objects they are bound to.

Let us consider a tuple t = (‘list_numbers’, [1, 2, 3])

The above tuple t contains elements of different data types, the first one is an immutable string and the second one is a mutable list. The tuple itself isn’t mutable. i.e. it doesn’t have any methods for changing its contents. Likewise, the string is immutable because strings don’t have any mutating methods. But the list object does have mutating methods, so it can be changed. This is a subtle point, but nonetheless important: the “value” of an immutable object can’t change, but it’s constituent objects can.

How objects are passed to Functions

It's important for us to know the difference between mutable and immutable types and how they are treated when passed onto functions. Memory efficiency is highly affected when the proper objects are used.

For example, if a mutable object is called by reference in a function, it can change the original variable itself. Hence to avoid this, the original variable needs to be copied to another variable. Immutable objects can be called by reference because its value cannot be changed anyways.

def update_List(list1):
list1 += [10]n = [5, 6]
print(id(n)) # 140312184155336updateList(n)
print(n) # [5, 6, 10]
print(id(n)) # 140312184155336

As we can see from the above example, we have called the list via call by reference, so the changes are made to the original list itself.

Lets take a look at another example:

def update_Number(n):
print(id(n))
n += 10b = 5
print(id(b)) # 10055680
update_Number(b) # 10055680
print(b) # 5

In the above example, the same object is passed to the function, but the variable's value doesn’t change even though the object is identical. This is called pass by value. So what is exactly happening here? When the value is called by the function, only the value of the variable is passed, not the object itself. So the variable referencing the object is not changed, but the object itself is being changed but within the function scope only. Hence the change is not reflected.

--

--