Python Aliasing Copyright © Software Carpentry 2010

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Python Programming Chapter 9: Tuples Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Lists Introduction to Computing Science and Programming I.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Mechanics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Lists in Python.
Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Browsing Directories Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution.
Interface and Implementation Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
First-Class Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
More Patterns Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Lists Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Indexing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Graphics Lab: MyPaint Dan Maselko 1. MyPaint Description  For this assignment you will be implementing a very simple paint program.  You should be able.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
String and Lists Dr. José M. Reyes Álamo.
Program Design Invasion Percolation: Aliasing
Program Design Invasion Percolation: Testing
Program Design Invasion Percolation: Resolving Ties
Program Design Invasion Percolation: Neighbors
2-D Lists Taken from notes by Dr. Neil Moore
CSC 108H: Introduction to Computer Programming
CSC 108H: Introduction to Computer Programming
Ruth Anderson CSE 140 University of Washington
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Bryan Burlingame 03 October 2018
6. Lists Let's Learn Python and Pygame
Return by Reference CSCE 121 J. Michael Moore.
Data Structures – 1D Lists
Chapter 8 Namespaces and Memory Realities
Hello World! Syntax.
Coding Concepts (Standards and Testing)
String and Lists Dr. José M. Reyes Álamo.
Version Control Basic Operation Copyright © Software Carpentry 2010
Ruth Anderson UW CSE 160 Winter 2017
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Version Control Introduction Copyright © Software Carpentry 2010
Nate Brunelle Today: Lists
Tuples.
Program Design Invasion Percolation: Bugs
Reference semantics, variables and names
Version Control Conflict Copyright © Software Carpentry 2010
Ruth Anderson CSE 160 University of Washington
Program Design Invasion Percolation: The Grid
Ruth Anderson UW CSE 160 Spring 2018
Browsing Directories Using walk
slides created by Ethan Apter
2-D Lists Taken from notes by Dr. Neil Moore
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

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 2

An alias is a second name for a piece of data Often easier (and more useful) than making a second copy 3

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 4

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 5

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 6

Aliasing happens whenever one variable's value is assigned to another variable 7

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

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

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 second 'isaac' 10

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

But lists are mutable 12

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

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

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

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

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

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

Example: use lists of lists to implement a 2D grid 19

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

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

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

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

# Correct code grid = [] for x in range(N): temp = [] for y in range(N): temp.append(1) grid.append(temp) 24

Outer "spine" of structure # Correct code grid = [] for x in range(N): temp = [] for y in range(N): temp.append(1) grid.append(temp) Outer "spine" of structure 25

Add N sub-lists to outer list # 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 26

Create a sublist of N 1's # 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 27

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

Last element of outer list is the sublist currently being filled in # 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 29

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

# Incorrect code grid = [] EMPTY = [] # Equivalent code 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) 31

Aren't meaningful variable names supposed to be a good thing? # 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? 32

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

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

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

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

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

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

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

No Aliasing first = [] second = [] 40

second = [] second = first No Aliasing Aliasing first = [] first = [] second = [] second = first 41

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

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

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

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

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

If aliasing can cause bugs, why allow it? 47

If aliasing can cause bugs, why allow it? Some languages don't 48

If aliasing can cause bugs, why allow it? Some languages don't Or at least appear not to 49

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

If aliasing can cause bugs, why allow it? Some languages don't Or at least appear not to Aliasing a million-element list is more efficient than copying it Sometimes really do want to update a structure in place 51

Greg Wilson created by 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.