Alias vs. copy

From Knowledge Kitchen
Jump to navigation Jump to search


Aliases

Aliases are two or more variables that point to the same value at the same spot in memory.

For example

#int example
x = 4
y = x 		#x and y are now aliases
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory
#float example
x = 4.2
y = x 		#x and y are now aliases
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory
#string example
x = "limerick"
y = x 		#x and y are now aliases
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory
#bool example
x = True
y = x 		#x and y are now aliases
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory
#NoneType example
x = None
y = x 		#x and y are now aliases
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory
#list example
x = [0,1,2]
y = x 		#x and y are now aliases
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory
#dictionary example
x = {"limerick": "jabberwocky"}
y = x 		#x and y are now aliases
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory


Immutable data types

Now look at this slight variation…

#int example
x = 4
y = 4
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory
#float example
x = 4.2
y = 4.2
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory
#string example
x = "limerick"
y = "limerick"
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory
#bool example
x = True
y = True
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory
#NoneType example
x = None
y = None
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints True since x and y point to the same location in memory


Mutable data types

Things are different with mutable data types.

Notice that the mutable data types (lists and dictionaries) in these examples are being made as copies (a.k.a. duplicates). Even though a given list or dictionary may have the same values in it as another list or dictionary already stored in memory, they are still stored in a separate spot in memory.

#list example
x = [0,1,2]
y = [0,1,2]
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints False since x and y point to different locations in memory - they are copies ( duplicates), not aliases
#dictionary example
x = {"limerick": "jabberwocky"}
y = {"limerick": "jabberwocky"}
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints False since x and y point to different locations in memory - they are copies ( duplicates), not aliases


It is not possible to create copies in different place in memory for immutable data types. This is why two immutable data type variables pointing to the same value are always aliases of each other and never copies.

Copies

There are other ways to get copies of mutable data types. For example

#list example
x = [0,1,2]
y = x.copy()
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints False since x and y point to different locations in memory - they are copies ( duplicates), not aliases
#list example
x = [0,1,2]
y = x[:] #doing a slice on a list always gets a copy
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints False since x and y point to different locations in memory - they are copies ( duplicates), not aliases
#dictionary example
x = {"limerick": "jabberwocky"}
y = x.copy() #gets a copy
print(x == y)	#prints True since x and y point to the same value
print(x is y)	#prints False since x and y point to different locations in memory - they are copies (duplicates), not aliases


What links here