Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
1 Value vs. reference semantics. Recall: Value semantics value semantics: Behavior where variables are copied when assigned to each other or passed as.
Classes 2 COMPSCI 105 SS 2015 Principles of Computer Science.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lecture 05 – Dictionaries.  Consider the following methods of sorting information in a sequence. What is the output produced by each program? 2COMPSCI.
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Lists Introduction to Computing Science and Programming I.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
Lists in Python.
Introduction COMPSCI 105 SS 2015 Principles of Computer Science.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lists Victor Norman CS104. Reading Quiz Q1: What is printed by the following statements? alist = [3, 67, “cat”, [56, 57, “dog”], [ ], 3.14, False] print(3.14.
Introduction. 2COMPSCI Computer Science Fundamentals.
Lecture 21 - Tuples COMPSCI 101 Principles of Programming.
UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
I Power Int 2 Computing Software Development High Level Language Constructs.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
CSC 107 – Programming For Science. The Week’s Goal.
Xin Liu Feb 4, * Use midterm questions for previous 231/217 midterms for practices * ams
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Lecture 04 – Models of memory Mutable and immutable data.
JSON COMPSCI 105 SS 2015 Principles of Computer Science.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Tuples Chapter 10 Python for Informatics: Exploring Information
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
Lists COMPSCI 105 S Principles of Computer Science.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
10. MORE OBJECTS Rocky K. C. Chang October 18, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and adapted from John.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
CS314 – Section 5 Recitation 9
Scientific Programming in Python -- Cheat Sheet
COMPSCI 107 Computer Science Fundamentals
CS 1110 Introduction to Programming Spring 2017
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Tuples Chapter 10 Python for Everybody
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Sharing, mutability, and immutability
Python for Informatics: Exploring Information
Sharing, mutability, and immutability
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
References.
Sharing, mutability, and immutability
Reference semantics, variables and names
Topics Basic String Operations String Slicing
Topics Basic String Operations String Slicing
CSE 231 Lab 7.
Variables and Constants
Objects Management.
Topics Basic String Operations String Slicing
Python fundamental.
Introduction to Computer Science
Presentation transcript:

Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science

Exercises  Work on PreLab01  id=104  What is the output of the following code fragments? Lecture 02COMPSCI 1052 def double(x): return x*2 my_list1 = [double(x) for x in range(10)] print(my_list1) def double(x): return x*2 my_list1 = [double(x) for x in range(10)] print(my_list1) my_list2 = [double(x) for x in range(10) if x%2==0] print(my_list2) my_list2 = [double(x) for x in range(10) if x%2==0] print(my_list2) names = ['Angela', 'Ann', 'Adriana'] my_list = [len(x) for x in names] print (my_list) names = ['Angela', 'Ann', 'Adriana'] my_list = [len(x) for x in names] print (my_list)

Modeling objects in memory  Value equality  Reference equality X y [1, 2, 3, 4, 5] X y Two different objects that store the same information. Two different references / names for the same object. >>> x = [1,2,3,4,5] >>> y = [1,2,3,4,5] >>> x = [1,2,3,4,5] >>> y = [1,2,3,4,5] >>> x = [1,2,3,4,5] >>> y = x >>> x = [1,2,3,4,5] >>> y = x Lecture 02COMPSCI 1053

Different ways to compare equality  ==  Calls a method of the object  Programmer who defined the object decides how to determine equality  Typically involves checking the contents of the objects  We should always use this for literals  is  Checks the references of the objects  Evaluates to True if they are the same object >>> x = [1,2,3,4,5] >>> y = [1,2,3,4,5] >>> x == y >>> x is y >>> x = [1,2,3,4,5] >>> y = [1,2,3,4,5] >>> x == y >>> x is y >>> x = [1,2,3,4,5] >>> y = x >>> x == y >>> x is y >>> x = [1,2,3,4,5] >>> y = x >>> x == y >>> x is y True False True False True Lecture 02COMPSCI 1054

Mutable and Immutable objects  An immutable object is an object whose state cannot be modified after it is created.  Examples of immutable objects:  integer, boolean, float, string, tuple  Examples of mutable objects  lists, dictionaries, sets, most data structures studied in this course  Tip: Whenever you call a method of an object, make sure you know if changes the contents of the object or returns a new object. >>> name = "Angela" >>> y = name.lower() >>> name = "Angela" >>> y = name.lower() nameBob >>> name = "Angela" >>> name = "Bob" >>> name = "Angela" >>> name = "Bob" a new String object is instantiated and given the data “Bob" during its construction nameAngela Return a new object nameAngela yangela Lecture 02COMPSCI 1055

String – Immutable objects  Every UNIQUE string you create will have it’s own address space in memory. Lecture 02COMPSCI 1056 >>> a = 'foo' >>> b = 'foo' >>> id(a) >>> id(b) >>> a is b True >>> a == b True >>> >>> a = 'foo' >>> b = 'foo' >>> id(a) >>> id(b) >>> a is b True >>> a == b True >>> Same memory location >>> x = [1,2,3] >>> y = [1,2,3] >>> id(x) >>> id(y) >>> x is y False >>> x == y True >>> x = [1,2,3] >>> y = [1,2,3] >>> id(x) >>> id(y) >>> x is y False >>> x == y True

Reversing a list  The function my_list.reverse() alters the content of my_list >>> x = [1, 2, 3] >>> y = x >>> x.reverse() >>> x = [1, 2, 3] >>> y = x >>> x.reverse() x y [1, 2, 3] >>> x >>> y [3, 2, 1] >>> x.reverse() changes the contents of the object Lecture 02COMPSCI 1057

Exercise 1  But …what is the output of the following code fragment? Lecture 02COMPSCI 1058 p = [1, 2, 3] print (p[::-1]) print (p) p = [1, 2, 3] print (p[::-1]) print (p)

Aliases  Two references to the same object are known as aliases  When an assignment is performed, the reference to the object on the right of the assignment is assigned to the variable on the left  When a method of an object is called, it sometimes returns a value and sometimes it alters the object x = [1, 2, 3, 4] y = x x.append(5) print(x) print(y) x = [1, 2, 3, 4] y = x x.append(5) print(x) print(y) [1, 2, 3, 4, 5] Alter the object Lecture 02COMPSCI 1059

Example  What happens in the following cases? What is the output? x = [1, 2, 3, 4] y = x x = x + [5] print(x) print(y) x = [1, 2, 3, 4] y = x x = x + [5] print(x) print(y) x = [1, 2, 3, 4] y = x x += [5] print(x) print(y) x = [1, 2, 3, 4] y = x x += [5] print(x) print(y) Alter the object Return a new object x [1,2,3,4,5] [1,2,3,4] y x y [1,2,3,4,5] Lecture 02COMPSCI Example01.py [1, 2, 3, 4, 5] [1, 2, 3, 4] [1, 2, 3, 4, 5] [1, 2, 3, 4] [1, 2, 3, 4, 5] But replaced

Shallow copies  Lists and dictionaries have a copy method  data.copy() x = [1, 2, 3, 4, 5] y = x.copy() print ( x is y ) print ( x == y) x = [1, 2, 3, 4, 5] y = x.copy() print ( x is y ) print ( x == y) a = [ [11], [22], [33] ] b = a.copy() print( a is b ) print( a[0] is b[0] ) a = [ [11], [22], [33] ] b = a.copy() print( a is b ) print( a[0] is b[0] ) False True False True [1,2,3,4, 5] y x False True False True Lecture 02COMPSCI Example01.py

Shallow copy  New object created  Contents of the original object are copied  If the contents are references, then the references are copied b[,, ] a [11][22][33] print( a is b ) print( a[0] is b[0] ) print( a is b ) print( a[0] is b[0] ) False True False True Lecture 02COMPSCI 10512

Deep copies  New object created  Contents of the original object are copied  If the contents are references, then the copy the objects referred to a[,, ] [11][22][33] b[,, ] [11][22][33] import copy a = [ [11], [22], [33] ] b = copy.deepcopy(a) print( a is b ) print( a[0] is b[0] ) import copy a = [ [11], [22], [33] ] b = copy.deepcopy(a) print( a is b ) print( a[0] is b[0] ) False Lecture 02COMPSCI Example01.py

Summary  Variables store references to the objects, not the actual objects  When you assign a variable, a reference is copied, not the object  There are two kinds of equality  Equality of content (value equality) can be tested with ==  Equality of identity (reference equality) can be tested with is  When a copy is created, it can be a shallow or deep copy  A shallow copy copies the references  A deep copy recursively copies the objects referred to Lecture 02COMPSCI 10514

Exercise 2  What is the output of the following code fragment? Lecture 02COMPSCI a = b = print (a is b) print (a == b) a = "foo" b = "foo" print (a is b) print (a == b) a = [1,2,3] b = [1,2,3] print (a is b) print (a == b) a = b = print (a is b) print (a == b) a = "foo" b = "foo" print (a is b) print (a == b) a = [1,2,3] b = [1,2,3] print (a is b) print (a == b)

Exercise 3  What is the output of the following code fragments? Lecture 02COMPSCI name = 'Angela' x = name name = 'Bob' print(name) print (x) print (name is x) print (name == x) name = 'Angela' print(name) print (x) print (name is x) print (name == x) name = 'Angela' x = name name = 'Bob' print(name) print (x) print (name is x) print (name == x) name = 'Angela' print(name) print (x) print (name is x) print (name == x) my_list = [1,2,3] y = my_list my_list = [4,5] print(my_list) print (y) print (my_list is y) print (my_list == y) my_list = [1,2,3] print(my_list) print (y) print (my_list is y) print (my_list == y) my_list = [1,2,3] y = my_list my_list = [4,5] print(my_list) print (y) print (my_list is y) print (my_list == y) my_list = [1,2,3] print(my_list) print (y) print (my_list is y) print (my_list == y)