Python
Aliasing
Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.
An alias is a second name for a piece of data
Python
Aliasing
An alias is a second name for a piece of data Often easier (and more useful) than making a second copy
Python
Aliasing
An alias is a second name for a piece of data Often easier (and more useful) than making a second copy If the data is immutable, aliases don't matter
Python
Aliasing
An alias is a second name for a piece of data Often easier (and more useful) than making a second copy If the data is immutable, aliases don't matter Because the data can't change
Python
Aliasing
An alias is a second name for a piece of data Often easier (and more useful) than making a second copy If the data is immutable, aliases don't matter Because the data can't change But if data can change, aliases can result in a lot of hard-to-find bugs
Python
Aliasing
Aliasing happens whenever one variable's value is assigned to another variable
Python
Aliasing
Aliasing happens whenever one variable's value is assigned to another variable
first = 'isaac'
variable first
value 'isaac'
Python
Aliasing
Aliasing happens whenever one variable's value is assigned to another variable
first = 'isaac' second = first
variable first second
value 'isaac'
Python
Aliasing
Aliasing happens whenever one variable's value is assigned to another variable
first = 'isaac' second = first
But as we've already seen
variable first second value 'isaac'
Python
Aliasing
Aliasing happens whenever one variable's value is assigned to another variable
first = 'isaac' second = first
But as we've already seen
first = first + ' newton' variable first second value 'isaac' 'isaac newton'
Python
Aliasing
But lists are mutable
Python
Aliasing
But lists are mutable
first = ['isaac']
variable first
value
'isaac'
Python
Aliasing
But lists are mutable
first = ['isaac'] second = first
variable first second
value
'isaac'
Python
Aliasing
But lists are mutable
first = ['isaac'] second = first first = first.append('newton') print first
['isaac', 'newton']
variable first second 'isaac' 'newton' value
Python
Aliasing
But lists are mutable
first = ['isaac'] second = first first = first.append('newton') print first
['isaac', 'newton']
print second
['isaac', 'newton']
variable first second
value
'isaac'
'newton'
Python
Aliasing
But lists are mutable
first = ['isaac'] second = first first = first.append('newton') print first
['isaac', 'newton']
print second
['isaac', 'newton']
variable first
value
Didn't explicitly modify second
second 'isaac' 'newton'
Python
Aliasing
But lists are mutable
first = ['isaac'] second = first first = first.append('newton') print first
['isaac', 'newton']
print second
['isaac', 'newton']
variable first
value
Didn't explicitly modify second A side effect
Python Aliasing
second 'isaac' 'newton'
Example: use lists of lists to implement a 2D grid
Python
Aliasing
Example: use lists of lists to implement a 2D grid
3 5 7
7 5 3
5 6 2
8 3 4
2 6 5
4 3 8
Python Aliasing
Example: use lists of lists to implement a 2D grid grid
7 5 3 5 6 2 8 3 4 3 5 7
2 6 5
4 3 8
Python Aliasing
Example: use lists of lists to implement a 2D grid grid[0]
7 5 3 5 6 2 8 3 4 3 5 7
2 6 5
4 3 8
Python Aliasing
Example: use lists of lists to implement a 2D grid grid[0][1]
7 5 3 5 6 2 8 3 4 3 5 7
2 6 5
4 3 8
Python Aliasing
# Correct code grid = [] for x in range(N): temp = [] for y in range(N): temp.append(1) grid.append(temp)
Python
Aliasing
# Correct code grid = [] for x in range(N): temp = [] for y in range(N): temp.append(1) grid.append(temp)
Outer "spine" of structure
Python
Aliasing
# Correct code grid = [] for x in range(N): temp = [] for y in range(N): temp.append(1) grid.append(temp)
Add N sub-lists to outer list
Python
Aliasing
# Correct code grid = [] for x in range(N): temp = [] for y in range(N): temp.append(1) grid.append(temp)
Create a sublist of N 1's
Python
Aliasing
# Equivalent code grid = [] for x in range(N): grid.append([]) for y in range(N): grid[-1].append(1)
Python
Aliasing
# Equivalent code grid = [] for x in range(N): grid.append([]) for y in range(N): grid[-1].append(1)
Last element of outer list is the sublist currently being filled in
Python
Aliasing
# Incorrect code grid = [] EMPTY = [] for x in range(N): grid.append(EMPTY) for y in range(N): grid[-1].append(1)
Python
Aliasing
# Incorrect code grid = [] EMPTY = [] for x in range(N): grid.append(EMPTY) for y in range(N): grid[-1].append(1)
# Equivalent code grid = [] for x in range(N): grid.append([]) for y in range(N): grid[-1].append(1)
Python
Aliasing
# Incorrect code grid = [] EMPTY = [] for x in range(N): grid.append(EMPTY) for y in range(N): grid[-1].append(1)
Aren't meaningful variable names supposed to be a good thing?
Python
Aliasing
variable x
value 0
grid = [] EMPTY = [] for x in range(N): grid.append(EMPTY) for y in range(N): grid[-1].append(1)
grid
EMPTY
Python
Aliasing
variable x
value 0
grid
grid = [] EMPTY = [] for x in range(N): grid.append(EMPTY) for y in range(N): grid[-1].append(1)
EMPTY
Python
Aliasing
variable x
value 0
grid
grid = [] EMPTY = [] for x in range(N): grid.append(EMPTY) for y in range(N): grid[-1].append(1)
EMPTY
Python
Aliasing
variable x
value 0
grid = [] EMPTY = [] for x in range(N): grid.append(EMPTY) for y in range(N): grid[-1].append(1)
grid
EMPTY
Python
Aliasing
variable x
value 0
grid = [] EMPTY = [] for x in range(N): grid.append(EMPTY) for y in range(N): grid[-1].append(1)
grid
EMPTY
Python
Aliasing
variable x
value 1
grid
grid = [] EMPTY = [] for x in range(N): grid.append(EMPTY) for y in range(N): grid[-1].append(1)
EMPTY
Python
Aliasing
variable x
value 1
grid
grid = [] EMPTY = [] for x in range(N): grid.append(EMPTY) for y in range(N): grid[-1].append(1)
EMPTY
You see the problem...
Python Aliasing
No Aliasing
first = [] second = []
Python
Aliasing
No Aliasing
Aliasing
first = [] second = []
first = [] second = first
Python
Aliasing
variable x
value 0
grid = [] for x in range(N): grid.append([]) for y in range(N): grid[-1].append(1)
grid
Python
Aliasing
variable x
value 0
grid = [] for x in range(N): grid.append([]) for y in range(N): grid[-1].append(1)
grid
Python
Aliasing
variable x
value 0
grid
grid = [] for x in range(N): grid.append([]) for y in range(N): grid[-1].append(1)
Python
Aliasing
variable x
value 1
grid = [] for x in range(N): grid.append([]) for y in range(N): grid[-1].append(1)
grid
1
Python
1
Aliasing
variable x
value 1
grid
grid = [] for x in range(N): grid.append([]) for y in range(N): grid[-1].append(1)
1
Python
1
Aliasing
If aliasing can cause bugs, why allow it?
Python
Aliasing
If aliasing can cause bugs, why allow it? 1. Some languages don't
Python
Aliasing
If aliasing can cause bugs, why allow it? 1. Some languages don't Or at least appear not to
Python
Aliasing
If aliasing can cause bugs, why allow it? 1. Some languages don't Or at least appear not to 2. Aliasing a million-element list is more efficient than copying it
Python
Aliasing
If aliasing can cause bugs, why allow it? 1. Some languages don't Or at least appear not to 2. Aliasing a million-element list is more efficient than copying it 3. Sometimes really do want to update a structure in place
Python
Aliasing
created by
Greg Wilson
October 2010
Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.