Everything Is Object
What's the definition of an object?
An object is anything with a fixed shape or form, that you can touch or see, and that is not alive.
This is in real life let's speak about Python now
In Python, everything is treated as an object. Every object has these three attributes:
While ID and Type cannot be changed once it’s created, values can be changed for Mutable objects.
Identity
An object’s identity never changes once it has been created. You may think of it as the object’s address in memory. The id() function returns an integer representing its identity.
The id() function returns a unique id for the specified object. All objects in Python have their own unique id. The id is assigned to the object when it is created
>>> a = 5
>>> id(a)
4494737776
Type
An object’s type defines the possible values and operations (e.g. “does it have a length?”) that type supports. An object type is unchangeable like the identity.
The type() function will provide the type of the object that’s provided as its argument.
>>> type(2)
<class 'int'>
>>> type(-6.25)
<class 'float'>
>>> type(2,)
<class 'tuple'>
>>> type("hello")
<class 'str'>
>>> type('A')
<class 'str'>
>>> type('346.789')
<class 'str'>
>>> type([2, 3, 4])
<class 'list'>
>>> type({'category': 'produce', 'count': 200})
<class 'dict'>
>>> type(print)
<class 'builtin_function_or_method'>
>>> type(type)
<class 'type'>)
Value
This refers to the value stored by the object. we can use print() to know the value of the object
>>>a = 5
>>>print(a)
5
Ok let us move up to another thing about objects immutable or mutable
Each variable we create in Python contains an instance of an object, in python, there are two types of objects (mutable and immutable objects).
Each time an instance is created a unique id is created, and the type is defined at runtime and cannot be changed later, but its state can be changed if it is a mutable object.
In short, mutable objects can change their contents and their state, and immutable objects cannot.
Mutable objects
list, dict, set
>>> my_list = ['cat', 'dog', 'horse']
>>> my_list
['cat', 'dog', 'bunny']
>>> print('Address of my_list is: {}'.format(id(my_list)))
Address of my_list is: 139929780579208]
>>> my_list[0] = 'tiger'
>>> my_list
['tiger', 'dog', 'horse']
>>> print('Address of my_list is: {}'.format(id(my_list)))
Address of my_list is: 139929780579208
If we want to change the first value in our list and print it out, we can see that the list changed, but the memory address of the list is the same. It changed the value in place. That’s what mutable means.
Immutable objects
integer, float, string, tuple, bool, frozenset
An immutable object is an object that is not changeable and its state cannot be modified after it is created. In Python, a string is immutable. You cannot overwrite the values of immutable objects.
>>> phrase = 'how you like me now'
>>> # let try to change a single character of the phrase.
>>> phrase[0] = 'D'
...
TypeError: 'str' object does not support item assignment
We get a TypeError because strings are immutable. We can’t change the string object.
why does it matter and how differently does Python treat mutable and immutable objects
Now we know the difference between mutable and immutable objects we know that immutable objects can be used to make sure that the object remains constant throughout the program. However, the mutable objects can be changed whether expected or not. Changing a mutable data type won’t change its memory address on the other hand changing an immutable type can produce errors.
How arguments are passed to functions and what does that imply for mutable and immutable objects
It's important 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 their value cannot be changed anyways.
>>> def increment(n)
... n += 1
...
>>> a = 3 # int is immutable
>>> increment(a)
>>> a
3
>>> def increment(l):
... l += [4]
...
>>> l = [1, 2, 3] # list is mutable
>>> increment(l)
>>> l
[1, 2, 3, 4]