References.

Slides:



Advertisements
Similar presentations
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Advertisements

Lists Introduction to Computing Science and Programming I.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
©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.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
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.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Built-in Data Structures in Python An Introduction.
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.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Python Functions.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Lecture 04 – Models of memory Mutable and immutable data.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
Python Programing: An Introduction to Computer Science
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.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
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.
Classes and Objects Digging a little deeper. Rectangles We want a class to represent a rectangle which located somewhere in XY plane. The question is,
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
Chapter 9 A Second Look at Classes and Objects - 2.
CS314 – Section 5 Recitation 9
Introduction to python programming
String and Lists Dr. José M. Reyes Álamo.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 2 Basic Computation
Introduction To Repetition The for loop
Containers and Lists CIS 40 – Introduction to Programming in Python
Arrays 2/4 By Pius Nyaanga.
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Variables and Primative Types
Mathcad Basics The Mathcad does not require any programming language to perform simple operations. The Mathcad follow the precedence of operation such.
Modified from Sharon Guffy
Ruth Anderson CSE 140 University of Washington
Advanced Programming Behnam Hatami Fall 2017.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CS190/295 Programming in Python for Life Sciences: Lecture 5
Basic notes on pointers in C
CS190/295 Programming in Python for Life Sciences: Lecture 6
Coding Concepts (Basics)
Forth A stack language.
String and Lists Dr. José M. Reyes Álamo.
Reference semantics, variables and names
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
CHAPTER 4: Lists, Tuples and Dictionaries
Ruth Anderson CSE 160 University of Washington
And now for something completely different . . .
Python Review
Names of variables, functions, classes
Topics Basic String Operations String Slicing
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
Objects Management.
Topics Basic String Operations String Slicing
C Parameter Passing.
Presentation transcript:

References

Simple values and collections >>> a = 5 >>> b = a >>> a = a + 1 >>> b 5 >>> a = [1, 2, 3] >>> b = a >>> a[1] = 5 >>> b [1, 5, 3] What’s going on?

Variable contents Think of a variable as a “box” that can hold a small amount of information In the case of a simple value (number or boolean), the value can fit in the box In the case of a larger value, such as a list, what is put in the box is a reference to the value What we copy to another variable is what is in the box

Call by value When we call a function, the arguments in the call are evaluated, and the values are put into the parameters If the parameters are changed within the function (which isn’t good style), the changes are not put back into the arguments Besides, the arguments are not necessarily simple variables; they could be literal numbers, or expressions >>> a = 5 >>> def alter(x): x = x + 1 >>> alter(a) >>> print(a) 5 We refer to this as call by value What is in a is a value, and that is what is passed to the function

Call by reference When a function is called with something other than a number or boolean, the same rules apply, but something different happens >>> b = [1, 2, 3] >>> def alter(x): x[1] = 99 # changes contents of b x = [4, 5, 6] # does not affect b >>> alter(b) >>> print(b) [1, 99, 3] This is call by reference What is in b is a reference, and that is what is passed to the function In this example b continues to refer to the same list (the assignment to x does not change that), but the list has been altered

Collections as parameters A variable is a “box” that can hold a small amount of information Four bytes of actual data, but there is associated information to describe the type of the data Dictionaries and sets are mutable: You can change the values in them Dictionaries and sets are not small, so like lists, a variable whose value appears to be a dictionary or set, is actually a reference to that dictionary or set Python keeps this straight, so you don’t (usually) have to Bottom line: Dictionaries and sets are like lists; when you pass one into a function, You can change the values in the dictionary or set, but You can’t change it to be a different dictionary or set

Strings as parameters Are strings passed to functions by value or by reference? >>> c = "abcde" >>> def alter(s): s = s + "123" >>> d = alter(c) >>> print(c) abcde >>> print(d) None Answer: Strings are passed by reference, but since they are immutable, nothing you do in the function will change the original string

Making lists You can enter a list directly: >>> [1, 2, 3] [1, 2, 3] You can give a sequence to the list function: >>> list(range(1, 4)) [1, 2, 3] >>> list({'one', 'two', 'three'}) ['two', 'three', ‘one'] >>> list({'one': 1, 'two':2}) ['two', ‘one'] You can “multiply” a list: >>> ['A'] * 3 ['A', 'A', ‘A'] >>> ['a', 'b', 'c'] * 2 ['a', 'b', 'c', 'a', 'b', 'c']

List multiplication gone wrong >>> m = [['a', 'b', 'c']] * 3 >>> m [['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', ‘c']] >>> m[1][2] = '*' >>> m [['a', 'b', '*'], ['a', 'b', '*'], ['a', 'b', '*']] Explanation: In the first line above, the list contains a reference to a list The *3 made copies of the reference, not copies of the list itself This gets assigned to m, which has three copies of the same reference The second assignment above changes the one referenced list

It’s all about memory Variables always hold small values--integers, floats, booleans, and references Small values are cheap to copy and pass around All other kinds of values are larger (some of which can grow and shrink, like lists), and these larger values are kept in a special part of memory called the heap Larger values are expensive (in both time and memory) to copy, so it doesn’t happen automatically Two ways to copy: A shallow copy of an object makes a copy of an object that includes all the small values (including references) in the original object A deep copy of an object makes a copy of that object that includes deep copies of all the referenced objects in the original object

copy and deepcopy >>> import copy >>> m = [1, [2, 3], 4] >>> m2 = copy.copy(m) >>> m3 = copy.deepcopy(m) >>> m[1][1] = 99 # change to a sublist >>> m.append(5) # change to top level >>> m [1, [2, 99], 4, 5] >>> m2 [1, [2, 99], 4] >>> m3 [1, [2, 3], 4]

Slicing Slicing (with the [i:j] notation) produces shallow copies Using [:] copies the entire list >>> m = [0, 1, 2, 3, 4, 5] >>> m2 = m[2:5] >>> m3 = m[:] >>> m[3] = 99 >>> m [0, 1, 2, 99, 4, 5] >>> m2 [2, 3, 4] >>> m3 [0, 1, 2, 3, 4, 5]

The End