What is Object:
- The concept of allocating the memory space for class at the time of execution of the program is known as object
(OR)
- Instance of a class is known as a object
Types of Objects:-
Python supports two
types of Objects. They are:
- Immutable Objects.
- Mutable Objects.
1).Immutable Objects
- The Objects which does not allow to modify the content are known as Immutable Objects.
- Whenever we try to modify the data of the Immutable Objects without modifying the data of those Objects new Objects will be created with the modified content.
Python immutable
Objects are:
- int
- float
- complex
- bool
- str
- tuple
x=10
print(x)
print(type(x))
print(id(x))
y=10
print(y)
print(type(y))
print(id(y))
Output:
10
<class 'int'>
1715414000
10
<class 'int'>
1715414000
Example 2:
a=10
print(a)
print(type(a))
print(id(a))
a=15
print(a)
print(type(a))
print(id(a))
Output:
10
<class 'int'>
1715414000
15
<class 'int'>
1715414160
Example 3:
str1="siva"
print(str1)
print(type(str1))
print(id(str1))
str2="siva"
print(str2)
print(type(str2))
print(id(str2))
Output:
siva
<class 'str'>
55428632
siva
<class 'str'>
55428632
Example 4:
str3="siva"
print(str3)
print(type(str3))
print(id(str3))
str3="krishna"
print(str3)
print(type(str3))
print(id(str3))
Output:
siva
<class 'str'>
55899784
krishna
<class 'str'>
55899896
Example 5:
tup1=(10,2.3,True,3+4j,'siva')
print(tup1)
print(id(tup1))
for p in tup1:
print(p)
print(id(p))
tup2=(10,2.3,True,3+4j,'siva')
print(tup2)
print(id(tup2))
for p in tup2:
print(p)
print(id(p))
Output:
(10, 2.3, True, (3+4j), 'siva')
48639584
10
1715414000
2.3
5810768
True
1715159456
(3+4j)
55239856
siva
55899784
(10, 2.3, True, (3+4j), 'siva')
55543504
10
1715414000
2.3
5810768
True
1715159456
(3+4j)
55241776
siva
55899784
Note:
- Before we are creating immutable object,Python interpreter checks already immutable object is created with the same content.
- If already immutable object is created with the same content without creating the new-object, already existing object address will be given to the second reference variable.
- Two immutable objects creates with same content is not possible.
- Immutable Objects will give better performance when compared to mutable Objects.
- We cannot create two different immutable Objects with the same content at any given point of time.
- Applying the iterations on immutable Objects will give better performance.
2).Mutable Objects :
- The Objects which we allows to modify the content are known as Mutable Objects.
Python mutable
Objects are:
- list
- set
- dict
Example 1:
x=[10,20,30]
print(x)
print(type(x))
print(id(x))
y=[10,20,30]
print(y)
print(type(y))
print(id(y))
Output:
[10, 20, 30]
<class 'list'>
58409544
[10, 20, 30]
<class 'list'>
55184136
Example 2:
a=[10,20,30]
print(a)
print(type(a))
print(id(a))
a=[40,50,60]
print(a)
print(type(a))
print(id(a))
Output:
[10, 20, 30]
<class 'list'>
55106952
[40, 50, 60]
<class 'list'>
55099720
Note:
- We can create two different mutable Objects with the same content.
- Two different variables having same content but create different objects.
No comments:
Post a Comment