Everything Is Object in Python
Everything Is Object in Python
Classes are objects, instances of classes are objects, modules are objects, and functions are
objects.
Anything that you can point a variable to is an object.
To make simple it will be easier to visualize an object as a box that contains data inside.
Once the object is created the object identity never changes. You might think of this identity
as the object’s memory address
Python id() function:
To know the identity of an object, we only have to use the id()function, which returns an
integer representing its address in memory.
The id() function returns a unique id for the specified object. All objects in Python has its
own unique id. The id is assigned to the object when it is created.
This is an integer that is unique for the given object and remains constant during its lifetime.
If we relate this to C, then they are actually the memory address, here in Python it is the
unique id.
Syntax : id(object)
We need to pass an object as parameter to the id() function where object can be an integer,
float, string, class, function, list, tuple, etc
Basic Example:
# Python program to illustrate id() function
a = 10
b = 11
c = 130.56
text = 'Hello'
Output :
ID of a = 9781088
ID of b = 9781120
ID of c = 139832028993808
ID of text = 139894857394352
There are few cases when the Python id() function assigns the exact same identification
number to multiple objects. This happens when the objects, like integers, floats, strings,
tuples are immutable.
numx=12
numy=12
txt1 = 'Python'
txt2 = 'Python'
print(id(numx))
print(id(numy))
print(id(txt1))
print(id(txt2))
print(id(my_tuple1))
print(id(my_tuple2))
Output:
1764024123920
1764024123920
140562250770160
140562250770160
140562250322496
140562250322496
The id() function works on the principle of caching which in turn works only on the objects that are
immutable. This helps Python preserve memory.
Python Memory Management
In C, when we assign a variable, we first declare it, thereby reserving a space in memory, and
then store the value in the memory spot allocated. We can create another variable with the
same value by repeating the process, ending up with two spots in memory, each with its
own value that is equivalent to the other’s.
Python employs a different approach. Instead of storing values in the memory space
reserved by the variable, Python has the variable refer to the value.
Similar to pointers in C, variables in Python refer to values (or objects) stored somewhere in
memory.
In fact, all variable names in Python are said to be references to the values, some of which
are front loaded by Python and therefore exist before the name references occur .
Making References to Values
Example
x=10
print(id(x))
print(id(y))
if id(x) == id(y):
Output :
D:\Jun2022>python ob.py
1682096062992
1682096062992
In the above example, y = x will create another reference variable y which will refer to the
same object because Python optimizes memory utilization by allocation the same object
reference to a new variable if the object already exists with the same value.
Now, let’s change the value of x and see what happens.
Example:
x = 10
y=x
print(id(x))
print(id(y))
if id(x) == id(y):
x = 20
print(id(x))
print(id(y))
if id(x) == id(y):
Output:
D:\Jun2022>python ob1.py
2257549984272
2257549984272
x and y refer to the same object
2257549984592
2257549984272
Each time we create a variable that refers to an object, a new object is created .
>>> nx=76543
>>> ny=76543
>>> print(nx)
76543
>>> print(ny)
76543
>>> print(id(nx))
1764025161968
>>> print(id(ny))
1764025162032
We can, however, have two variables refer to the same object through a process called
“aliasing”: assigning one variable the value of the other variable. In other words, one
variable now serves as an alias for the other, since both of them now refer to the same
object.
>>> x=987654321
>>> print(id(x))
1764025162064
>>> y=x
>>> print(id(y))
1764025162064
While it is true that a new object is created each time we have a variable that makes
reference to it, there are few notable exceptions:
1.Some strings
2. Integers between -5 and 255 (inclusive). For small integers (-5...255), Python3 keeps an
integer pool - so these small integers feels like being cached
3. Empty Tuples
String interning
Interpreter Mode Examples 1 and 2:
Example 1:
>>> st1='python is easy to learn'
>>> print(st1)
>>> print(st2)
1764029247968
>>> print(id(st2))
1764029570048
Example 2:
>>> s1='python'
>>> s2='python'
>>> print(s1)
python
>>> print(s2)
python
>>> print(id(s1))
1764029591856
>>> print(id(s2))
1764029591856
print(str1)
print(str2)
print(id(str1))
print(id(str2))
Output:
D:\Jun2022>python str.py
This is a result of string interning, which allows two variables to refer to the same string
object. Python automatically does this, although the exact rules remain not clear