Python Aliasing Copyright Software Carpentry 2010 This work

  • Slides: 52
Download presentation
Python Aliasing Copyright © Software Carpentry 2010 This work is licensed under the Creative

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 Python Aliasing

An alias is a second name for a piece of data Often easier (and

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

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

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

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 Python Aliasing

Aliasing happens whenever one variable's value is assigned to another variable first = 'isaac'

Aliasing happens whenever one variable's value is assigned to another variable first = 'isaac' Python variable value first 'isaac' Aliasing

Aliasing happens whenever one variable's value is assigned to another variable first = 'isaac'

Aliasing happens whenever one variable's value is assigned to another variable first = 'isaac' second = first variable value first 'isaac' second Python Aliasing

Aliasing happens whenever one variable's value is assigned to another variable first = 'isaac'

Aliasing happens whenever one variable's value is assigned to another variable first = 'isaac' second = first But as we've already seen… variable value first 'isaac' second Python Aliasing

Aliasing happens whenever one variable's value is assigned to another variable first = 'isaac'

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 Python value first 'isaac' second 'isaac newton' Aliasing

But lists are mutable Python Aliasing

But lists are mutable Python Aliasing

But lists are mutable first = ['isaac'] variable value first 'isaac' Python Aliasing

But lists are mutable first = ['isaac'] variable value first 'isaac' Python Aliasing

But lists are mutable first = ['isaac'] second = first variable value first second

But lists are mutable first = ['isaac'] second = first variable value first second 'isaac' Python Aliasing

But lists are mutable first = ['isaac'] second = first. append('newton') print first ['isaac',

But lists are mutable first = ['isaac'] second = first. append('newton') print first ['isaac', 'newton'] variable value first second 'isaac' Python 'newton' Aliasing

But lists are mutable first = ['isaac'] second = first. append('newton') print first ['isaac',

But lists are mutable first = ['isaac'] second = first. append('newton') print first ['isaac', 'newton'] print second variable ['isaac', 'newton'] value first second 'isaac' Python 'newton' Aliasing

But lists are mutable first = ['isaac'] second = first. append('newton') print first ['isaac',

But lists are mutable first = ['isaac'] second = first. append('newton') print first ['isaac', 'newton'] print second variable ['isaac', 'newton'] value first Didn't explicitly modify second Python second 'isaac' 'newton' Aliasing

But lists are mutable first = ['isaac'] second = first. append('newton') print first ['isaac',

But lists are mutable first = ['isaac'] second = first. append('newton') print first ['isaac', 'newton'] print second variable ['isaac', 'newton'] value first Didn't explicitly modify second 'isaac' 'newton' A side effect Python Aliasing

Example: use lists of lists to implement a 2 D grid Python Aliasing

Example: use lists of lists to implement a 2 D grid Python Aliasing

Example: use lists of lists to implement a 2 D grid 3 5 7

Example: use lists of lists to implement a 2 D grid 3 5 7 7 5 8 2 5 6 3 2 4 5 4 3 8 Python Aliasing

Example: use lists of lists to implement a 2 D grid 3 5 grid

Example: use lists of lists to implement a 2 D grid 3 5 grid 7 7 5 8 2 5 6 3 2 4 5 4 3 8 Python Aliasing

Example: use lists of lists to implement a 2 D grid 3 5 grid[0]

Example: use lists of lists to implement a 2 D grid 3 5 grid[0] 7 7 5 8 2 5 6 3 2 4 5 4 3 8 Python Aliasing

Example: use lists of lists to implement a 2 D grid 3 5 grid[0][1]

Example: use lists of lists to implement a 2 D grid 3 5 grid[0][1] 7 7 5 8 2 5 6 3 2 4 5 4 3 8 Python Aliasing

# Correct code grid = [] for x in range(N): temp = [] for

# 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 = [] Outer "spine" of structure for x in range(N):

# Correct code grid = [] Outer "spine" of structure 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

# Correct code grid = [] for x in range(N): temp = [] for y in range(N): temp. append(1) grid. append(temp) Python Add N sub-lists to outer list Aliasing

# Correct code grid = [] for x in range(N): temp = [] for

# Correct code grid = [] for x in range(N): temp = [] for y in range(N): temp. append(1) grid. append(temp) Python Create a sublist of N 1's Aliasing

# Equivalent code grid = [] for x in range(N): grid. append([]) for y

# 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

# 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.

# 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.

# Incorrect code grid = [] EMPTY = [] for x in range(N): grid. append(EMPTY) for y in range(N): grid[-1]. append(1) Python # Equivalent code grid = [] for x in range(N): grid. append([]) for y in range(N): grid[-1]. append(1) Aliasing

# Incorrect code grid = [] EMPTY = [] for x in range(N): grid.

# Incorrect code grid = [] EMPTY = [] for x in range(N): grid. append(EMPTY) for y in range(N): grid[-1]. append(1) Python Aren't meaningful variable names supposed to be a good thing? Aliasing

variable x grid value 0 grid = [] EMPTY = [] for x in

variable x grid value 0 grid = [] EMPTY = [] for x in range(N): grid. append(EMPTY) for y in range(N): grid[-1]. append(1) EMPTY Python Aliasing

variable x grid value 0 grid = [] EMPTY = [] for x in

variable x grid value 0 grid = [] EMPTY = [] for x in range(N): grid. append(EMPTY) for y in range(N): grid[-1]. append(1) EMPTY Python Aliasing

variable value x 0 y 0 grid = [] EMPTY = [] for x

variable value x 0 y 0 grid = [] EMPTY = [] for x in range(N): grid. append(EMPTY) for y in range(N): grid[-1]. append(1) EMPTY Python Aliasing

variable value x 0 y 0 grid = [] EMPTY = [] for x

variable value x 0 y 0 grid = [] EMPTY = [] for x in range(N): grid. append(EMPTY) for y in range(N): grid[-1]. append(1) grid EMPTY 1 Python Aliasing

variable value x 0 y 2 grid = [] EMPTY = [] for x

variable value x 0 y 2 grid = [] EMPTY = [] for x in range(N): grid. append(EMPTY) for y in range(N): grid[-1]. append(1) grid EMPTY 1 1 1 Python Aliasing

variable value x 1 y 2 grid = [] EMPTY = [] for x

variable value x 1 y 2 grid = [] EMPTY = [] for x in range(N): grid. append(EMPTY) for y in range(N): grid[-1]. append(1) grid EMPTY 1 1 1 Python Aliasing

variable value x 1 y 2 grid = [] EMPTY = [] for x

variable value x 1 y 2 grid = [] EMPTY = [] for x in range(N): grid. append(EMPTY) for y in range(N): grid[-1]. append(1) grid EMPTY 1 1 1 You see the problem. . . Python Aliasing

No Aliasing first = [] second = [] Python Aliasing

No Aliasing first = [] second = [] Python Aliasing

Python No Aliasing first = [] second = first Aliasing

Python No Aliasing first = [] second = first Aliasing

variable x grid Python value 0 grid = [] for x in range(N): grid.

variable x grid Python value 0 grid = [] for x in range(N): grid. append([]) for y in range(N): grid[-1]. append(1) Aliasing

variable x grid Python value 0 grid = [] for x in range(N): grid.

variable x grid Python value 0 grid = [] for x in range(N): grid. append([]) for y in range(N): grid[-1]. append(1) Aliasing

variable value x 0 y 2 grid = [] for x in range(N): grid.

variable value x 0 y 2 grid = [] for x in range(N): grid. append([]) for y in range(N): grid[-1]. append(1) grid 1 1 1 Python Aliasing

variable value x 1 y 2 grid = [] for x in range(N): grid.

variable value x 1 y 2 grid = [] for x in range(N): grid. append([]) for y in range(N): grid[-1]. append(1) 1 1 1 Python Aliasing

variable value x 1 y 0 grid = [] for x in range(N): grid.

variable value x 1 y 0 grid = [] for x in range(N): grid. append([]) for y in range(N): grid[-1]. append(1) grid 1 1 Python Aliasing

If aliasing can cause bugs, why allow it? Python 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 Python Aliasing

If aliasing can cause bugs, why allow it? 1. Some languages don't 2. Python

If aliasing can cause bugs, why allow it? 1. Some languages don't 2. Python Or at least appear not to Aliasing

If aliasing can cause bugs, why allow it? 1. Some languages don't 2. Or

If aliasing can cause bugs, why allow it? 1. Some languages don't 2. Or at least appear not to 2. Aliasing a million-element list is more efficient 3. Python than copying it Aliasing

If aliasing can cause bugs, why allow it? 1. Some languages don't 2. Or

If aliasing can cause bugs, why allow it? 1. Some languages don't 2. Or at least appear not to 2. Aliasing a million-element list is more efficient 3. 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

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.