aliasing vs cloning in python

Publicado em: 13 Junho 2020
no canal de: Tarun Sir
764
12

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


Nesta página do site você pode assistir ao vídeo on-line aliasing vs cloning in python duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Tarun Sir 13 Junho 2020, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 764 vezes e gostou 12 espectadores. Boa visualização!