Hello, Nice to meet you, I’m an object.

INTRODUCTION

ID AND TYPE, WHAT IS THAT?

>>> i = "Hello"
>>> id(i)
***704
>>> j = "World"
>>> id(j)
***736 (different from ***704)

As you can see the id of i and j is different and unique, but what happens if i assign two objects to point to the same thing:

>>> i = "Hello"
>>> j = "World"
>>> id(i)
***139725014575280
>>> id(j)
***139725014575280 (same id)

So the two of them are the same because they are refering to the same value, so Python optimizes resources by making two names that refer to the same string value refer to the same object, this is known as an alias.

Now let’s talk about type, in Python it is used to “return the type of an object” and its “return value is a type object and generally the same object as returned by object.__class__. So, if you wanted to find out what type is your variable, you can do this:

>>> obj1 = "Julien"
>>> type(obj1)
<class 'str'>

MUTABLE OBJECTS

You might think that strings are mutable objects, but what happens if you concatenate the string with another string?

>>> str1 = "Julien might win at Starcraft"
>>> str2 = " or he might lose."
>>> str1 += str2
>>> print(str1)
Julien might win at Starcraft or he might lose.

A third string is created when you concatenate the two strings, but what if the strings are the same? If they are the same, in memory, a pointer will point to the spot. For example, using id:

# Assigning two objects to the same string
>>> a = "b"
>>> b = "b"
>>> id(a), id(b)
(139873424855424, 139873424855424)# Both strings have the same id# Adding third object 'c'>>> c = "c"
>>> id(c)
139873424786912
>>> a += c
>>> print(a)
bc
>>> id(a)
139873423703824

INMUTABLE OBJECTS

>>> x = 24601
>>> x
24601

>>> x = 24602
>>> x
24602

Well, it seems that we changed x successfully. This is exactly where many people get confused. What exactly happened under the hood here? Let's use id to further investigate:

>>> x = 24601
>>> x
24601

>>> id(x)
1470416816

>>> x = 24602
>>> x
24602

>>> id(x)
1470416832

So we can see that by assigning x = 24602, we didn't change the value of the object that x had been bound to before. Rather, we created a new object, and bound the name x to it.

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

As discussed earlier, Python containers liked tuples are immutable. That means value of a tuple can't be changed after it is created. But the "value" of a tuple is infact 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 = (‘holberton’, [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 ARGUMENTS ARE PASSED TO FUNCTIONS AND WHAT DOES THAT IMPLY FOR MUTABLE AND INMUTABLE OBJECTS

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 updateList(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.

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

In the above example the same object is passed to the function, but the variables 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.