In this video you will learn about aliasing and cloning of objects in python.
alias in python
Assignment operator in python do not create copies of objects, instead they only bind a new name to an existing object. The new name is called the alias and this process is called the aliasing. The object and its alias have the same memory address/reference. Consider the following example:
x=[10,20,30]
Here x is a list.
y=x, adds the name y, to the same location which is represented by x.
So if we change : y[0]=100, then x[0] is also changed as, x and y are only two different names, representing the same object in memory.
If we print x[0] or y[0] both will print the 100.
If we compare x and y using == it gives true as both have same values, if we compare them using is operator it also gives True because both x and y are same objects.
Note: Here problem is that both x and y referring to same objects, so if we make change in one other is also altered. If we do not want to change the original list when we change new list then we use concept of cloning.
Cloning in python
The cloning of list can be achieved using the copy() function or using the list slicing. Consider the following example:
x=[10,20,30]
Here x is a list.
y=x.copy(), creates a new instance, having same values as x, but x and y points to two different objects in the memory.
So if we change : y[0]=100, x is not affected.
If we compare x and y using == it gives true as both have same values, if we compare them using is operator it gives False because both x and y are not same objects.
#python_by_tarun_sir #tarun_sir #python #tarun_sir_python #python_video_71 #aliasing_in_python #python_tutorial #cloning_in_python
На этой странице сайта вы можете посмотреть видео онлайн aliasing vs cloning in python длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Tarun Sir 13 Июнь 2020, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 764 раз и оно понравилось 12 зрителям. Приятного просмотра!