EVERYTHING IS OBJECT IN PYTHON.

EVERYTHING IS OBJECT IN PYTHON.

After reading this blog, you will understand what python is, id and type, mutable and immutable objects, how python treats them, and finally how arguments are passed to functions and what that implies for mutable and immutable objects.





An Object is a subset of a Class. A class is similar to a blueprint, whereas an instance is a copy of the class that contains actual values. Python is an object-oriented programming language that focuses on objects, rather than functions. Objects are essentially a single entity that encapsulates data variables and methods that act on that data.


Mutable Objects

The objects that can be changed after creation are known as mutable objects. Lists, arrays, set, dictionary, byte array, etc., are some of the mutable data types in Python.


Usually, mutable objects are used whenever there is a need to change the size or content of the object during runtime.


Example

List

myList = ["One", 1, False, 'c']

myList.pop()

print(myList)

myList.append(True)

print(myList)

myList[0] = 2

print(myList)

?


Output:


['One', 1, False]

['One', 1, False, True]

[2, 1, False, True]

In the list, we can add and remove elements by using append() and pop() methods, respectively. We can also modify its content. So the list is a mutable data type.?


Dictionary

myDict = { "India" : "Delhi",?

??????????"isAisan" : True

}

myDict.update({"States" : 29})

print(myDict)

myDict["India"] = "New Delhi"

print(myDict)

Output:


{'India': 'Delhi', 'isAisan': True, 'States': 29}

{'India': 'New Delhi', 'isAisan': True, 'States': 29}

As seen in the example, we can add an element in the dictionary by using the add() function, and we can also change its content after creation. Therefore, the Python dictionary is mutable.


Sets

Like the dictionary, the set is also mutable.


mySet = set(('India', 'U.S.A', False, 1, False))

mySet.add(False)

print(mySet)

?


Output


{False, 1, 'U.S.A', 'India'}

?


Immutable Objects

Immutable objects can't be changed after creation. Strings, bytes, tuples, and frozen sets are examples of immutable data types.


Immutable objects are faster than mutable objects.


Example


Tuples

mytuple = ('a', 1, 2, 'b')

print(mytuple[0])

mytuple[0] = 'b'

Output


mytuple[0] = 'b'

TypeError: 'tuple' object does not support item assignment

As seen in the example, we can access the elements of tuples using the indices, but we can't modify them, so it is immutable.


Strings

myString = "myName"

print(myString[2])

myString[2] = 'n'

Output


myString[2] = 'n'

TypeError: 'str' object does not support item assignment

?


Similar to the tuple, we can't modify the strings. So, strings are also immutable.


why does it matter and how differently does Python treat mutable and immutable objects

Python works differently on mutable and immutable objects, the Immutable objects are quicker to access and are expensive to change because it involves the creation of a copy y the mutable objects are easy to change.


If the size or content of the object needs to be changed, it is recommended to use mutable objects.


Exception: Immutable objects have an exception, we know that a tuple in Python is Immutable, but the tuple contains a sequence of names with immutable links to objects that can be mutable.


tupla1 = ("klich", [1, 2, 3])?

The tuple1 is not mutable, but it contains a list in its element [1], and the lists if it is mutable and its content can change. As a rule of thumb, Generally, Primitive-like types are probably immutable and Customized Container-like types are mostly mutable.


How objects are passed on to functions

Since we know the difference between mutable and immutable types, let's look at how these are treated when they are switched to functions. The efficiency of memory is greatly affected when the right objects are used.


We walk through a list with a call for a reference and see how the changes affect the original list.


#example call by refernce


>>> def call_by_refernce(l1):

... ? ? l1 += [8]

...?

>>> list1 = [2, 4, 6]

>>> list1

[2, 4, 6]

>>> id(list1)

140494953940936

>>> call_by_refernce(list1)

>>> list1

[2, 4, 6, 8]

>>> id(list1)

140494953940936

>>>?

For example, if a mutable object is called by reference in a function, you can change the original variable itself. Therefore, to avoid this, the original variable must be copied to another variable. Immutable objects can be called by reference because their value cannot be changed anyway.


Let's look at this other example.


#example pass by value


>>> def pass_by_value(number):

... ? ? print(id(number))

... ? ? number += 8

... ? ? print(number)

...?

>>> x = 2

>>> id(x)

10914528

>>> pass_by_value(x)

0914528

10

>>> x

2

>>>?

We observe that the same object is passed to the function, but the value of the variable does not change, even if the object is identical (same id), this is what is called step by value.


What exactly is going on?

When the function invokes the value, only the value of the variable (2) is passed, not the object itself (x), so the variable that refers to the object is not changed, but the object itself is changed, but this occurs only within the scope of the function, so the change is not reflected.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了