61A Lecture 14 Announcements Mutable Functions A Function with - - PowerPoint PPT Presentation

61a lecture 14 announcements mutable functions a function
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 14 Announcements Mutable Functions A Function with - - PowerPoint PPT Presentation

61A Lecture 14 Announcements Mutable Functions A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 4 A Function with Behavior That Varies Over Time Let's model a bank account that has a balance


slide-1
SLIDE 1

61A Lecture 14

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Mutable Functions

slide-4
SLIDE 4

A Function with Behavior That Varies Over Time

Let's model a bank account that has a balance of $100

4

slide-5
SLIDE 5

A Function with Behavior That Varies Over Time

>>> withdraw(25) Let's model a bank account that has a balance of $100

4

slide-6
SLIDE 6

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 Let's model a bank account that has a balance of $100

4

slide-7
SLIDE 7

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 Let's model a bank account that has a balance of $100 Argument:
 amount to withdraw

4

slide-8
SLIDE 8

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 Let's model a bank account that has a balance of $100 Argument:
 amount to withdraw Return value: remaining balance

4

slide-9
SLIDE 9

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 >>> withdraw(25) 50 Let's model a bank account that has a balance of $100 Argument:
 amount to withdraw Return value: remaining balance

4

slide-10
SLIDE 10

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 >>> withdraw(25) 50 Let's model a bank account that has a balance of $100 Argument:
 amount to withdraw Return value: remaining balance

4

Second withdrawal of the same amount

slide-11
SLIDE 11

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 >>> withdraw(25) 50 Let's model a bank account that has a balance of $100 Argument:
 amount to withdraw Return value: remaining balance Different return value!

4

Second withdrawal of the same amount

slide-12
SLIDE 12

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 >>> withdraw(25) 50 >>> withdraw(60) Let's model a bank account that has a balance of $100 Argument:
 amount to withdraw Return value: remaining balance Different return value!

4

Second withdrawal of the same amount

slide-13
SLIDE 13

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 >>> withdraw(25) 50 >>> withdraw(60) 'Insufficient funds' Let's model a bank account that has a balance of $100 Argument:
 amount to withdraw Return value: remaining balance Different return value!

4

Second withdrawal of the same amount

slide-14
SLIDE 14

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 >>> withdraw(25) 50 >>> withdraw(60) 'Insufficient funds' Let's model a bank account that has a balance of $100 Argument:
 amount to withdraw Return value: remaining balance Different return value! Where's this balance stored?

4

Second withdrawal of the same amount

slide-15
SLIDE 15

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 >>> withdraw(25) 50 >>> withdraw(60) 'Insufficient funds' >>> withdraw = make_withdraw(100) Let's model a bank account that has a balance of $100 Argument:
 amount to withdraw Return value: remaining balance Different return value! Where's this balance stored?

4

Second withdrawal of the same amount

slide-16
SLIDE 16

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 >>> withdraw(25) 50 >>> withdraw(60) 'Insufficient funds' >>> withdraw = make_withdraw(100) Let's model a bank account that has a balance of $100 Argument:
 amount to withdraw Return value: remaining balance Different return value! Where's this balance stored? Within the parent frame

  • f the function!

4

Second withdrawal of the same amount

slide-17
SLIDE 17

A Function with Behavior That Varies Over Time

>>> withdraw(25) 75 >>> withdraw(25) 50 >>> withdraw(60) 'Insufficient funds' >>> withdraw = make_withdraw(100) Let's model a bank account that has a balance of $100 Argument:
 amount to withdraw Return value: remaining balance Different return value! Where's this balance stored? Within the parent frame

  • f the function!

4

Second withdrawal of the same amount A function has a body and a parent environment

slide-18
SLIDE 18

Persistent Local State Using Environments

5

Interactive Diagram

slide-19
SLIDE 19

Persistent Local State Using Environments

The parent frame contains the balance, the local state of the withdraw function

5

Interactive Diagram

slide-20
SLIDE 20

Persistent Local State Using Environments

The parent frame contains the balance, the local state of the withdraw function Every call decreases the same balance by (a possibly different) amount

5

Interactive Diagram

slide-21
SLIDE 21

Persistent Local State Using Environments

The parent frame contains the balance, the local state of the withdraw function Every call decreases the same balance by (a possibly different) amount

5

All calls to the same function have the same parent Interactive Diagram

slide-22
SLIDE 22

Reminder: Local Assignment

6

Interactive Diagram

slide-23
SLIDE 23

Reminder: Local Assignment

Assignment binds name(s) to value(s) in the first frame of the current environment

6

Interactive Diagram

slide-24
SLIDE 24

Reminder: Local Assignment

Assignment binds name(s) to value(s) in the first frame of the current environment

6

Interactive Diagram

slide-25
SLIDE 25

Reminder: Local Assignment

Execution rule for assignment statements: Assignment binds name(s) to value(s) in the first frame of the current environment

6

Interactive Diagram

slide-26
SLIDE 26

Reminder: Local Assignment

Execution rule for assignment statements:

  • 1. Evaluate all expressions right of =, from left to right
  • 2. Bind the names on the left to the resulting values in the current frame

Assignment binds name(s) to value(s) in the first frame of the current environment

6

Interactive Diagram

slide-27
SLIDE 27

Non-Local Assignment & Persistent Local State

7

slide-28
SLIDE 28

Non-Local Assignment & Persistent Local State

def make_withdraw(balance):

7

slide-29
SLIDE 29

Non-Local Assignment & Persistent Local State

def make_withdraw(balance): """Return a withdraw function with a starting balance."""

7

slide-30
SLIDE 30

Non-Local Assignment & Persistent Local State

def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount):

7

slide-31
SLIDE 31

Non-Local Assignment & Persistent Local State

def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance

7

slide-32
SLIDE 32

Non-Local Assignment & Persistent Local State

def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance if amount > balance:

7

slide-33
SLIDE 33

Non-Local Assignment & Persistent Local State

def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance if amount > balance: return 'Insufficient funds'

7

slide-34
SLIDE 34

Non-Local Assignment & Persistent Local State

def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount

7

slide-35
SLIDE 35

Non-Local Assignment & Persistent Local State

def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount return balance

7

slide-36
SLIDE 36

Non-Local Assignment & Persistent Local State

def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount return balance return withdraw

7

slide-37
SLIDE 37

Non-Local Assignment & Persistent Local State

def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount return balance return withdraw Declare the name "balance" nonlocal at the top of the body of the function in which it is re-assigned

7

slide-38
SLIDE 38

Non-Local Assignment & Persistent Local State

def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount return balance return withdraw Declare the name "balance" nonlocal at the top of the body of the function in which it is re-assigned Re-bind balance in the first non-local frame in which it was bound previously

7

slide-39
SLIDE 39

Non-Local Assignment & Persistent Local State

def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount return balance return withdraw Declare the name "balance" nonlocal at the top of the body of the function in which it is re-assigned Re-bind balance in the first non-local frame in which it was bound previously

7

(Demo)

slide-40
SLIDE 40

Non-Local Assignment

slide-41
SLIDE 41

The Effect of Nonlocal Statements

nonlocal <name>

9

slide-42
SLIDE 42

The Effect of Nonlocal Statements

Effect: Future assignments to that name change its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name>

9

slide-43
SLIDE 43

The Effect of Nonlocal Statements

Effect: Future assignments to that name change its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name> Python Docs: an "enclosing scope"

9

slide-44
SLIDE 44

The Effect of Nonlocal Statements

Effect: Future assignments to that name change its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name>, <name>, ... Python Docs: an "enclosing scope"

9

slide-45
SLIDE 45

The Effect of Nonlocal Statements

From the Python 3 language reference: Effect: Future assignments to that name change its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name>, <name>, ... Python Docs: an "enclosing scope"

9

slide-46
SLIDE 46

The Effect of Nonlocal Statements

From the Python 3 language reference: Names listed in a nonlocal statement must refer to pre-existing bindings in an enclosing scope. Effect: Future assignments to that name change its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name>, <name>, ... Python Docs: an "enclosing scope"

9

slide-47
SLIDE 47

The Effect of Nonlocal Statements

From the Python 3 language reference: Names listed in a nonlocal statement must refer to pre-existing bindings in an enclosing scope. Names listed in a nonlocal statement must not collide with pre-existing bindings in the local scope. Effect: Future assignments to that name change its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name>, <name>, ... Python Docs: an "enclosing scope"

9

slide-48
SLIDE 48

The Effect of Nonlocal Statements

From the Python 3 language reference: Names listed in a nonlocal statement must refer to pre-existing bindings in an enclosing scope. Names listed in a nonlocal statement must not collide with pre-existing bindings in the local scope. Effect: Future assignments to that name change its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name>, <name>, ... Python Docs: an "enclosing scope"

9

Current frame

slide-49
SLIDE 49

The Effect of Nonlocal Statements

From the Python 3 language reference: Names listed in a nonlocal statement must refer to pre-existing bindings in an enclosing scope. Names listed in a nonlocal statement must not collide with pre-existing bindings in the local scope.

http://docs.python.org/release/3.1.3/reference/simple_stmts.html#the-nonlocal-statement

Effect: Future assignments to that name change its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name>, <name>, ... Python Docs: an "enclosing scope"

9

Current frame

slide-50
SLIDE 50

The Effect of Nonlocal Statements

http://www.python.org/dev/peps/pep-3104/

From the Python 3 language reference: Names listed in a nonlocal statement must refer to pre-existing bindings in an enclosing scope. Names listed in a nonlocal statement must not collide with pre-existing bindings in the local scope.

http://docs.python.org/release/3.1.3/reference/simple_stmts.html#the-nonlocal-statement

Effect: Future assignments to that name change its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name>, <name>, ... Python Docs: an "enclosing scope"

9

Current frame

slide-51
SLIDE 51

The Many Meanings of Assignment Statements

x = 2

10

slide-52
SLIDE 52

The Many Meanings of Assignment Statements

x = 2 Status Effect

10

slide-53
SLIDE 53

The Many Meanings of Assignment Statements

x = 2 Status Effect

  • No nonlocal statement
  • "x" is not bound locally

10

slide-54
SLIDE 54

The Many Meanings of Assignment Statements

x = 2 Status Effect

  • No nonlocal statement
  • "x" is not bound locally

Create a new binding from name "x" to object 2 in the first frame of the current environment

10

slide-55
SLIDE 55

The Many Meanings of Assignment Statements

x = 2 Status Effect

  • No nonlocal statement
  • "x" is not bound locally

Create a new binding from name "x" to object 2 in the first frame of the current environment

  • No nonlocal statement
  • "x" is bound locally

10

slide-56
SLIDE 56

The Many Meanings of Assignment Statements

x = 2 Status Effect

  • No nonlocal statement
  • "x" is not bound locally

Create a new binding from name "x" to object 2 in the first frame of the current environment

  • No nonlocal statement
  • "x" is bound locally

Re-bind name "x" to object 2 in the first frame

  • f the current environment

10

slide-57
SLIDE 57

The Many Meanings of Assignment Statements

x = 2 Status Effect

  • No nonlocal statement
  • "x" is not bound locally

Create a new binding from name "x" to object 2 in the first frame of the current environment

  • No nonlocal statement
  • "x" is bound locally

Re-bind name "x" to object 2 in the first frame

  • f the current environment
  • nonlocal x
  • "x" is bound in a non-local

frame

10

slide-58
SLIDE 58

The Many Meanings of Assignment Statements

x = 2 Status Effect

  • No nonlocal statement
  • "x" is not bound locally

Create a new binding from name "x" to object 2 in the first frame of the current environment

  • No nonlocal statement
  • "x" is bound locally

Re-bind name "x" to object 2 in the first frame

  • f the current environment
  • nonlocal x
  • "x" is bound in a non-local

frame Re-bind "x" to 2 in the first non-local frame of the current environment in which "x" is bound

10

slide-59
SLIDE 59

The Many Meanings of Assignment Statements

x = 2 Status Effect

  • No nonlocal statement
  • "x" is not bound locally

Create a new binding from name "x" to object 2 in the first frame of the current environment

  • No nonlocal statement
  • "x" is bound locally

Re-bind name "x" to object 2 in the first frame

  • f the current environment
  • nonlocal x
  • "x" is not bound in a non-

local frame

  • nonlocal x
  • "x" is bound in a non-local

frame Re-bind "x" to 2 in the first non-local frame of the current environment in which "x" is bound

10

slide-60
SLIDE 60

The Many Meanings of Assignment Statements

x = 2 Status Effect

  • No nonlocal statement
  • "x" is not bound locally

Create a new binding from name "x" to object 2 in the first frame of the current environment

  • No nonlocal statement
  • "x" is bound locally

Re-bind name "x" to object 2 in the first frame

  • f the current environment
  • nonlocal x
  • "x" is not bound in a non-

local frame SyntaxError: no binding for nonlocal 'x' found

  • nonlocal x
  • "x" is bound in a non-local

frame Re-bind "x" to 2 in the first non-local frame of the current environment in which "x" is bound

10

slide-61
SLIDE 61

The Many Meanings of Assignment Statements

x = 2 Status Effect

  • No nonlocal statement
  • "x" is not bound locally

Create a new binding from name "x" to object 2 in the first frame of the current environment

  • No nonlocal statement
  • "x" is bound locally

Re-bind name "x" to object 2 in the first frame

  • f the current environment
  • nonlocal x
  • "x" is bound in a 


non-local frame

  • "x" also bound locally
  • nonlocal x
  • "x" is not bound in a non-

local frame SyntaxError: no binding for nonlocal 'x' found

  • nonlocal x
  • "x" is bound in a non-local

frame Re-bind "x" to 2 in the first non-local frame of the current environment in which "x" is bound

10

slide-62
SLIDE 62

The Many Meanings of Assignment Statements

x = 2 Status Effect

  • No nonlocal statement
  • "x" is not bound locally

Create a new binding from name "x" to object 2 in the first frame of the current environment

  • No nonlocal statement
  • "x" is bound locally

Re-bind name "x" to object 2 in the first frame

  • f the current environment
  • nonlocal x
  • "x" is bound in a 


non-local frame

  • "x" also bound locally

SyntaxError: name 'x' is parameter and nonlocal

  • nonlocal x
  • "x" is not bound in a non-

local frame SyntaxError: no binding for nonlocal 'x' found

  • nonlocal x
  • "x" is bound in a non-local

frame Re-bind "x" to 2 in the first non-local frame of the current environment in which "x" is bound

10

slide-63
SLIDE 63

Python Particulars

11

slide-64
SLIDE 64

Python Particulars

Python pre-computes which frame contains each name before executing the body of a function.

11

slide-65
SLIDE 65

Python Particulars

Python pre-computes which frame contains each name before executing the body of a function. Within the body of a function, all instances of a name must refer to the same frame.

11

slide-66
SLIDE 66

Python Particulars

Python pre-computes which frame contains each name before executing the body of a function. Within the body of a function, all instances of a name must refer to the same frame.

11

Interactive Diagram

slide-67
SLIDE 67

Python Particulars

Python pre-computes which frame contains each name before executing the body of a function. Within the body of a function, all instances of a name must refer to the same frame. Local assignment

11

Interactive Diagram

slide-68
SLIDE 68

Python Particulars

Python pre-computes which frame contains each name before executing the body of a function. Within the body of a function, all instances of a name must refer to the same frame. Local assignment

11

Interactive Diagram

slide-69
SLIDE 69

Mutable Values & Persistent Local State

Mutable values can be changed without a nonlocal statement.

12

slide-70
SLIDE 70

Mutable Values & Persistent Local State

Mutable values can be changed without a nonlocal statement.

12

slide-71
SLIDE 71

Mutable Values & Persistent Local State

Mutable values can be changed without a nonlocal statement.

12

Name bound

  • utside of

withdraw def

slide-72
SLIDE 72

Mutable Values & Persistent Local State

Mutable values can be changed without a nonlocal statement.

12

Name bound

  • utside of

withdraw def Element assignment changes a list

slide-73
SLIDE 73

Mutable Values & Persistent Local State

Mutable values can be changed without a nonlocal statement.

12

Name bound

  • utside of

withdraw def Element assignment changes a list Interactive Diagram

slide-74
SLIDE 74

Mutable Values & Persistent Local State

Mutable values can be changed without a nonlocal statement. Name-value binding cannot change because there is no nonlocal statement

12

Name bound

  • utside of

withdraw def Element assignment changes a list Interactive Diagram

slide-75
SLIDE 75

Mutable Values & Persistent Local State

Mutable values can be changed without a nonlocal statement. Name-value binding cannot change because there is no nonlocal statement Mutable value can change

12

Name bound

  • utside of

withdraw def Element assignment changes a list Interactive Diagram

slide-76
SLIDE 76

Multiple Mutable Functions

(Demo)

slide-77
SLIDE 77

Referential Transparency, Lost

14

Interactive Diagram

slide-78
SLIDE 78

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an expression with its value

does not change the meaning of a program.

14

Interactive Diagram

slide-79
SLIDE 79

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an expression with its value

does not change the meaning of a program. mul(add(2, mul(4, 6)), add(3, 5))

14

Interactive Diagram

slide-80
SLIDE 80

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an expression with its value

does not change the meaning of a program. mul(add(2, mul(4, 6)), add(3, 5)) mul(add(2, 24 ), add(3, 5))

14

Interactive Diagram

slide-81
SLIDE 81

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an expression with its value

does not change the meaning of a program. mul(add(2, mul(4, 6)), add(3, 5)) mul(add(2, 24 ), add(3, 5)) mul( 26 , add(3, 5))

14

Interactive Diagram

slide-82
SLIDE 82

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an expression with its value

does not change the meaning of a program.

  • Mutation operations violate the condition of referential transparency because they do

more than just return a value; they change the environment. mul(add(2, mul(4, 6)), add(3, 5)) mul(add(2, 24 ), add(3, 5)) mul( 26 , add(3, 5))

14

Interactive Diagram

slide-83
SLIDE 83

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an expression with its value

does not change the meaning of a program.

  • Mutation operations violate the condition of referential transparency because they do

more than just return a value; they change the environment. mul(add(2, mul(4, 6)), add(3, 5)) mul(add(2, 24 ), add(3, 5)) mul( 26 , add(3, 5))

14

Interactive Diagram

slide-84
SLIDE 84

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an expression with its value

does not change the meaning of a program.

  • Mutation operations violate the condition of referential transparency because they do

more than just return a value; they change the environment. mul(add(2, mul(4, 6)), add(3, 5)) mul(add(2, 24 ), add(3, 5)) mul( 26 , add(3, 5))

14

Interactive Diagram