0% found this document useful (0 votes)
213 views11 pages

Everything Is Object in Python

1. In Python, everything is an object including integers, floats, strings, data structures, functions, classes, instances of classes, and modules. 2. Objects have an identity, type, and value. An object's identity is its memory address which can be retrieved using the id() function. 3. Variables in Python refer to objects rather than storing values directly. When a variable is assigned to an object, it creates a reference to that object. If another variable is assigned the same object, both variables refer to the same object.

Uploaded by

vsnpradeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
213 views11 pages

Everything Is Object in Python

1. In Python, everything is an object including integers, floats, strings, data structures, functions, classes, instances of classes, and modules. 2. Objects have an identity, type, and value. An object's identity is its memory address which can be retrieved using the id() function. 3. Variables in Python refer to objects rather than storing values directly. When a variable is assigned to an object, it creates a reference to that object. If another variable is assigned the same object, both variables refer to the same object.

Uploaded by

vsnpradeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Topic 6 : For Python, Everything is an Object

In Python, everything is an object.

All data in a Python program is represented by objects or by relationships between objects.


Booleans, integers, floats, strings, data structures, program functions… all of them are
implemented as an object.

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.

Each of these objects/boxes has an identity, a type, and a value.

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'

print('ID of a =', id(a))


print('ID of b =', id(b))
print('ID of c =', id(c))

print('ID of text =', id(text))

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'

my_tuple1 = ('A', 'B', 'C')

my_tuple2 = ('A', 'B', 'C')

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

When x = 10 is executed an integer object 10 is created in memory and its reference is


assigned to variable x, this is because everything is object in Python.
Example:
x = 10
y=x

print(id(x))

print(id(y))

if id(x) == id(y):

print("x and y refer to the same object")

Output :

D:\Jun2022>python ob.py

1682096062992
1682096062992

x and y refer to the same object

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):

print("x and y refer to the same object")

x = 20

print(id(x))
print(id(y))

if id(x) == id(y):

print("x and y refer to the same object")


else:

print("x and y does not refer to the same object")

Output:

D:\Jun2022>python ob1.py

2257549984272
2257549984272
x and y refer to the same object

2257549984592
2257549984272

x and y does not refer to the same object


Python keeps an internal counter on how many references an object has. Once the counter
goes to zero — meaning that no reference is made to the object — the garbage collector in
Python removes the object , thus freeing up the memory.

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

Exceptions with Immutable Objects

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

These exceptions arise as a result of memory optimization in Python implementation. After


all, if two variables refer to objects with the same value, why wasting memory creating a
new object for the second variable? Why not simply have the second variable refer to the
same object in memory ?
Let's see some examples

String interning
Interpreter Mode Examples 1 and 2:
Example 1:
>>> st1='python is easy to learn'

>>> st2='python is easy to learn'

>>> print(st1)

python is easy to learn

>>> print(st2)

python is easy to learn


>>> print(id(st1))

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

Both strings s1 and s2 are having the same id


Example 3 : Script mode

str1='Python is easy to learn and easy to write code'

str2='Python is easy to learn and easy to write code'

print(str1)
print(str2)

print(id(str1))
print(id(str2))

Output:

D:\Jun2022>python str.py

Python is easy to learn and easy to write code

Python is easy to learn and easy to write code


2483703305136
2483703305136

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

You might also like