Reference semantics, variables and names

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

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.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Data Types Integer 15 StringHelloThere! Float/Real BooleanYes / No CharP.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants.
Introduction to Python I CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
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.
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Intro Python: Variables, Indexing, Numbers, Strings.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Lecture 04 – Models of memory Mutable and immutable data.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Python I Some material adapted from Upenn cmpe391 slides and other sources.
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.
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
1 Python Data Structures LING 5200 Computational Corpus Linguistics Martha Palmer.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
String and Lists Dr. José M. Reyes Álamo.
Python Aliasing Copyright © Software Carpentry 2010
10 - Python Dictionary John R. Woodward.
Topic: Python’s building blocks -> Statements
Data Types and Conversions, Input from the Keyboard
When to use Tuples instead of Lists
Student Book An Introduction
PHP-language Variables, assingment and arithmetic operations
Repetition: the for loop
String Manipulation.
Basic notes on pointers in C
Sharing, mutability, and immutability
Understanding Assignment
Python I Some material adapted from Upenn cmpe391 slides and other sources.
More Loop Examples Functions and Parameters
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Sharing, mutability, and immutability
Programming Funamental slides
Transforming Data (Python®)
4. sequence data type Rocky K. C. Chang 16 September 2018
Chapter 8 Namespaces and Memory Realities
Variables Title slide variables.
Sharing, mutability, and immutability
Python I Some material adapted from Upenn cmpe391 slides and other sources.
String and Lists Dr. José M. Reyes Álamo.
Python I Some material adapted from Upenn cmpe391 slides and other sources.
References.
Sharing, mutability, and immutability
CHAPTER 4: Lists, Tuples and Dictionaries
And now for something completely different . . .
Chapter 9: Pointers and String
Variables and Computer Memory
Python Review
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
Objects Management.
Python fundamental.
Python Creating a calculator.
Getting Started in Python
Presentation transcript:

Reference semantics, variables and names Some material adapted from Upenn cmpe391 slides and other sources

Understanding Reference Semantics Assignment manipulates references x = y does not make a copy of the object y references x = y makes x reference the object y references Very useful; but beware!, e.g. >>> a = [1, 2, 3] # a now references the list [1, 2, 3] >>> b = a # b now references what a references >>> a.append(4) # this changes the list a references >>> print b # if we print what b references, [1, 2, 3, 4] # SURPRISE! It has changed… Why?

Understanding Reference Semantics There’s a lot going on with x = 3 An integer 3 is created and stored in memory A name x is created An reference to the memory location storing the 3 is then assigned to the name x So: When we say that the value of x is 3 we mean that x now refers to the integer 3 Type: Integer Data: 3 Name: x Ref: <address1> name list memory

Understanding Reference Semantics The data 3 we created is of type integer – objects are typed, variables are not In Python, the datatypes integer, float, and string (and tuple) are “immutable” This doesn’t mean we can’t change the value of x, i.e. change what x refers to … For example, we could increment x: >>> x = 3 >>> x = x + 1 >>> print x 4

Understanding Reference Semantics When we increment x, then what happens is: The reference of name x is looked up. The value at that reference is retrieved. Type: Integer Data: 3 Name: x Ref: <address1> >>> x = x + 1

Understanding Reference Semantics When we increment x, then what happening is: The reference of name x is looked up. The value at that reference is retrieved. The 3+1 calculation occurs, producing a new data element 4 which is assigned to a fresh memory location with a new reference Type: Integer Data: 3 Name: x Ref: <address1> Type: Integer Data: 4 >>> x = x + 1

Understanding Reference Semantics When we increment x, then what happening is: The reference of name x is looked up. The value at that reference is retrieved. The 3+1 calculation occurs, producing a new data element 4 which is assigned to a fresh memory location with a new reference The name x is changed to point to new ref Type: Integer Data: 3 Name: x Ref: <address1> Type: Integer Data: 4 >>> x = x + 1

Assignment So, for simple built-in datatypes (integers, floats, strings) assignment behaves as expected >>> x = 3 # Creates 3, name x refers to 3 >>> y = x # Creates name y, refers to 3 >>> y = 4 # Creates ref for 4. Changes y >>> print x # No effect on x, still ref 3 3

Assignment So, for simple built-in datatypes (integers, floats, strings) assignment behaves as expected >>> x = 3 # Creates 3, name x refers to 3 >>> y = x # Creates name y, refers to 3 >>> y = 4 # Creates ref for 4. Changes y >>> print x # No effect on x, still ref 3 3 Name: x Ref: <address1> Type: Integer Data: 3

Assignment So, for simple built-in datatypes (integers, floats, strings) assignment behaves as expected >>> x = 3 # Creates 3, name x refers to 3 >>> y = x # Creates name y, refers to 3 >>> y = 4 # Creates ref for 4. Changes y >>> print x # No effect on x, still ref 3 3 Name: x Ref: <address1> Type: Integer Data: 3 Name: y Ref: <address2>

Assignment So, for simple built-in datatypes (integers, floats, strings) assignment behaves as expected >>> x = 3 # Creates 3, name x refers to 3 >>> y = x # Creates name y, refers to 3 >>> y = 4 # Creates ref for 4. Changes y >>> print x # No effect on x, still ref 3 3 Name: x Ref: <address1> Type: Integer Data: 3 Name: y Ref: <address2> Type: Integer Data: 4

Assignment So, for simple built-in datatypes (integers, floats, strings) assignment behaves as expected >>> x = 3 # Creates 3, name x refers to 3 >>> y = x # Creates name y, refers to 3 >>> y = 4 # Creates ref for 4. Changes y >>> print x # No effect on x, still ref 3 3 Name: x Ref: <address1> Type: Integer Data: 3 Name: y Ref: <address2> Type: Integer Data: 4

Assignment So, for simple built-in datatypes (integers, floats, strings) assignment behaves as expected >>> x = 3 # Creates 3, name x refers to 3 >>> y = x # Creates name y, refers to 3 >>> y = 4 # Creates ref for 4. Changes y >>> print x # No effect on x, still ref 3 3 Name: x Ref: <address1> Type: Integer Data: 3 Name: y Ref: <address2> Type: Integer Data: 4

Assignment So, for simple built-in datatypes (integers, floats, strings) assignment behaves as expected >>> x = 3 # Creates 3, name x refers to 3 >>> y = x # Creates name y, refers to 3 >>> y = 4 # Creates ref for 4. Changes y >>> print x # No effect on x, still ref 3 3 Name: x Ref: <address1> Type: Integer Data: 3 Name: y Ref: <address2> Type: Integer Data: 4

Assignment & mutable objects For other data types (lists, dictionaries, user-defined types), assignment works differently These datatypes are “mutable” Change occur in place We don’t copy them into a new memory address each time If we type y=x and then modify y, both x and y are changed >>> x = 3 x = some mutable object >>> y = x y = x >>> y = 4 make a change to y >>> print x look at x 3 x will be changed as well immutable mutable

Why? Changing a Shared List 1 2 3 a = [1, 2, 3] a 1 2 3 b b = a a 1 2 3 a.append(4) 4 b

Surprising example surprising no more So now, here’s our code: >>> a = [1, 2, 3] # a now references the list [1, 2, 3] >>> b = a # b now references what a references >>> a.append(4) # this changes the list a references >>> print b # if we print what b references, [1, 2, 3, 4] # SURPRISE! It has changed…