14
UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Embed Size (px)

Citation preview

Page 1: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

UW CSE 190p Section

8/2, Summer 2012Dun-Yu Hsiao

Page 2: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Outlines

• Function: id(.)• Mutable and immutable types

• Quiz walkthrough

• Homework question

Page 3: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Memory Visualization

Page 4: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Function: id(.)

• Return the “identity” of an object.• Show the address of the object in memory.• An integer (or long integer) which is

guaranteed to be unique and constant for this object during its lifetime.

Page 5: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Immutability

• By immutability, we mean that whenever we start manipulating a immutable item, Python will spawn off another value for us.

Page 6: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Immutability Example

x = 300y = 300

s = 'a string'

t = ('a', 'tuple')

Page 7: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Mutability

• For mutable types, we can actually change the value where it's stored in memory.

Page 8: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Mutability Example

• l = [1, 2, 3]

Page 9: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Mutable and Immutable Types

• Mutable types:– list, dictionary

• Immutable types:– Int, float, string, tuple

Page 10: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Quiz Walkthrough

Assume these definitions:

y = 4

def double(y): return y + y

Evaluate the following expression. Show every step.

y + double(3 * y)

Page 11: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Quiz Walkthrough

y = 3 def double(y): return y + y def quadruple1(y): return double(double(y)) def quadruple2(y): return double(y) + double(y) def quadruple3(y): return y + y + y + y

double(y) + quadruple1(double(y) + y)

Page 12: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Quiz Walkthrough

y = 3 def double(y): return y + y def quadruple1(y): return double(double(y)) def quadruple2(y): return double(y) + double(y) def quadruple3(y): return y + y + y + y

double(y) + quadruple2(double(y) + y)

Page 13: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Quiz Walkthrough

y = 3 def double(y): return y + y def quadruple1(y): return double(double(y)) def quadruple2(y): return double(y) + double(y) def quadruple3(y): return y + y + y + y

double(y) + quadruple3(double(y) + y)

Page 14: UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao

Homework/Quiz Questions?