Python is an Object Oriented Programming (OOP) language — what does that mean? It means the Python programming language consists of
objects, which allows the user to have their own methods and attributes without having to re-create them each time. Python is built off of a data structure called the
PyObject, which is what all the data types inherit from — hence why the language consists of
ID AND TYPE, WHAT IS THAT?
id and type are objects, an
id “returns the identity of an object” and each one is “unique and constant” for the object as long as you use it. For example:
>>> i = "Hello"
>>> j = "World"
***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 (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"
Mutable objects are objects whose values can change — examples of mutable objects are
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
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
# 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"
>>> a += c
What about immutable objects? Immutable objects consists of
frozen set, and
bytes. These are objects that can’t be manipulated.
>>> x = 24601
>>> 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 = 24602
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
Its important for us to know 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.
list1 += 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.
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.