Python Alias
Python Alias
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.
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
Python
Aliasing
variable first
value 'isaac'
Python
Aliasing
value 'isaac'
Python
Aliasing
Python
Aliasing
Python
Aliasing
Python
Aliasing
variable first
value
'isaac'
Python
Aliasing
value
'isaac'
Python
Aliasing
['isaac', 'newton']
variable first second 'isaac' 'newton' value
Python
Aliasing
['isaac', 'newton']
print second
['isaac', 'newton']
value
'isaac'
'newton'
Python
Aliasing
['isaac', 'newton']
print second
['isaac', 'newton']
variable first
value
Python
Aliasing
['isaac', 'newton']
print second
['isaac', 'newton']
variable first
value
Python
Aliasing
7 5 3
5 6 2
8 3 4
2 6 5
4 3 8
Python Aliasing
2 6 5
4 3 8
Python Aliasing
2 6 5
4 3 8
Python Aliasing
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)
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)
Python
Aliasing
Python
Aliasing
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)
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
variable x
value 0
grid
EMPTY
Python
Aliasing
variable x
value 0
grid
EMPTY
Python
Aliasing
variable x
value 0
grid
EMPTY
Python
Aliasing
variable x
value 0
grid
EMPTY
Python
Aliasing
variable x
value 0
grid
EMPTY
Python
Aliasing
variable x
value 1
grid
EMPTY
Python
Aliasing
variable x
value 1
grid
EMPTY
No Aliasing
first = [] second = []
Python
Aliasing
No Aliasing
Aliasing
first = [] second = []
Python
Aliasing
variable x
value 0
grid
Python
Aliasing
variable x
value 0
grid
Python
Aliasing
variable x
value 0
grid
Python
Aliasing
variable x
value 1
grid
1
Python
1
Aliasing
variable x
value 1
grid
1
Python
1
Aliasing
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.