61A Lecture 13 Wednesday, September 26 A Function with Behavior - - PowerPoint PPT Presentation

61a lecture 13
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 13 Wednesday, September 26 A Function with Behavior - - PowerPoint PPT Presentation

61A Lecture 13 Wednesday, September 26 A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 2 A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100


slide-1
SLIDE 1

61A Lecture 13

Wednesday, September 26

slide-2
SLIDE 2

A Function with Behavior That Varies Over Time

2

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

slide-3
SLIDE 3

A Function with Behavior That Varies Over Time

2

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

slide-4
SLIDE 4

A Function with Behavior That Varies Over Time

2

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

slide-5
SLIDE 5

A Function with Behavior That Varies Over Time

2

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

  • f the same amount

Return value: remaining balance

slide-6
SLIDE 6

A Function with Behavior That Varies Over Time

2

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

  • f the same amount

Return value: remaining balance Different return value!

slide-7
SLIDE 7

A Function with Behavior That Varies Over Time

2

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

  • f the same amount

Return value: remaining balance Different return value!

slide-8
SLIDE 8

A Function with Behavior That Varies Over Time

2

>>> 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 Second withdrawal

  • f the same amount

Return value: remaining balance Different return value!

slide-9
SLIDE 9

A Function with Behavior That Varies Over Time

2

>>> withdraw(25) 75 >>> withdraw(25) 50 >>> withdraw(60) 'Insufficient funds' >>> withdraw(15) Let's model a bank account that has a balance of $100 Argument: amount to withdraw Second withdrawal

  • f the same amount

Return value: remaining balance Different return value!

slide-10
SLIDE 10

A Function with Behavior That Varies Over Time

2

>>> withdraw(25) 75 >>> withdraw(25) 50 >>> withdraw(60) 'Insufficient funds' >>> withdraw(15) 35 Let's model a bank account that has a balance of $100 Argument: amount to withdraw Second withdrawal

  • f the same amount

Return value: remaining balance Different return value!

slide-11
SLIDE 11

A Function with Behavior That Varies Over Time

2

>>> withdraw(25) 75 >>> withdraw(25) 50 >>> withdraw(60) 'Insufficient funds' >>> withdraw(15) 35 Let's model a bank account that has a balance of $100 Argument: amount to withdraw Second withdrawal

  • f the same amount

Return value: remaining balance Different return value! Where's this balance stored?

slide-12
SLIDE 12

A Function with Behavior That Varies Over Time

2

>>> withdraw(25) 75 >>> withdraw(25) 50 >>> withdraw(60) 'Insufficient funds' >>> withdraw(15) 35 >>> withdraw = make_withdraw(100) Let's model a bank account that has a balance of $100 Argument: amount to withdraw Second withdrawal

  • f the same amount

Return value: remaining balance Different return value! Where's this balance stored?

slide-13
SLIDE 13

A Function with Behavior That Varies Over Time

2

>>> withdraw(25) 75 >>> withdraw(25) 50 >>> withdraw(60) 'Insufficient funds' >>> withdraw(15) 35 >>> withdraw = make_withdraw(100) Let's model a bank account that has a balance of $100 Argument: amount to withdraw Second withdrawal

  • f the same amount

Return value: remaining balance Different return value! Where's this balance stored? Within the function!

slide-14
SLIDE 14

Persistent Local State

3

http://goo.gl/StRZP

slide-15
SLIDE 15

Persistent Local State

3

http://goo.gl/StRZP

A function with a parent frame

slide-16
SLIDE 16

Persistent Local State

3

http://goo.gl/StRZP

A function with a parent frame The parent contains local state

slide-17
SLIDE 17

Persistent Local State

3

http://goo.gl/StRZP

A function with a parent frame The parent contains local state Every call changes the balance

slide-18
SLIDE 18

Reminder: Local Assignment

4

Example: http://goo.gl/wcF71

slide-19
SLIDE 19

Reminder: Local Assignment

4

Example: http://goo.gl/wcF71

Assignment binds names to values in the current local frame

slide-20
SLIDE 20

Reminder: Local Assignment

4

Execution rule for assignment statements:

Example: http://goo.gl/wcF71

Assignment binds names to values in the current local frame

slide-21
SLIDE 21

Reminder: Local Assignment

4

Execution rule for assignment statements:

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

first frame of the current environment.

Example: http://goo.gl/wcF71

Assignment binds names to values in the current local frame

slide-22
SLIDE 22

Non-Local Assignment & Persistent Local State

5

slide-23
SLIDE 23

Non-Local Assignment & Persistent Local State

5

def make_withdraw(balance):

slide-24
SLIDE 24

Non-Local Assignment & Persistent Local State

5

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

slide-25
SLIDE 25

Non-Local Assignment & Persistent Local State

5

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

slide-26
SLIDE 26

Non-Local Assignment & Persistent Local State

5

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

slide-27
SLIDE 27

Non-Local Assignment & Persistent Local State

5

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

slide-28
SLIDE 28

Non-Local Assignment & Persistent Local State

5

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

slide-29
SLIDE 29

Non-Local Assignment & Persistent Local State

5

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

slide-30
SLIDE 30

Non-Local Assignment & Persistent Local State

5

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 Declare the name "balance" nonlocal

slide-31
SLIDE 31

Non-Local Assignment & Persistent Local State

5

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 Declare the name "balance" nonlocal Re-bind balance where it was bound previously

slide-32
SLIDE 32

Non-Local Assignment & Persistent Local State

5

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 Declare the name "balance" nonlocal Re-bind balance where it was bound previously

slide-33
SLIDE 33

Non-Local Assignment & Persistent Local State

5

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 Re-bind balance where it was bound previously

slide-34
SLIDE 34

Non-Local Assignment & Persistent Local State

5

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 Re-bind balance where it was bound previously Demo

slide-35
SLIDE 35

The Effect of Nonlocal Statements

6

nonlocal <name>

slide-36
SLIDE 36

The Effect of Nonlocal Statements

6

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

slide-37
SLIDE 37

The Effect of Nonlocal Statements

6

Effect: Future references to that name refer to 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"

slide-38
SLIDE 38

The Effect of Nonlocal Statements

6

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

slide-39
SLIDE 39

The Effect of Nonlocal Statements

6

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

slide-40
SLIDE 40

The Effect of Nonlocal Statements

6

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

slide-41
SLIDE 41

The Effect of Nonlocal Statements

6

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 references to that name refer to its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name> , <name 2>, ... Python Docs: an "enclosing scope"

slide-42
SLIDE 42

The Effect of Nonlocal Statements

6

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 references to that name refer to its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name> , <name 2>, ... Python Docs: an "enclosing scope"

slide-43
SLIDE 43

The Effect of Nonlocal Statements

6

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 references to that name refer to its pre-existing binding in the first non-local frame of the current environment in which that name is bound. nonlocal <name> , <name 2>, ... Python Docs: an "enclosing scope"

slide-44
SLIDE 44

The Many Meanings of Assignment Statements

7

x = 2

slide-45
SLIDE 45

The Many Meanings of Assignment Statements

7

x = 2 Status Effect

slide-46
SLIDE 46

The Many Meanings of Assignment Statements

7

x = 2 Status Effect

  • No nonlocal statement
  • "x" is not bound locally
slide-47
SLIDE 47

The Many Meanings of Assignment Statements

7

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.

slide-48
SLIDE 48

The Many Meanings of Assignment Statements

7

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
slide-49
SLIDE 49

The Many Meanings of Assignment Statements

7

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 of the current env.

slide-50
SLIDE 50

The Many Meanings of Assignment Statements

7

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 of the current env.

  • nonlocal x
  • "x" is bound in a

non-local frame

slide-51
SLIDE 51

The Many Meanings of Assignment Statements

7

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 of the current env.

  • 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 it is bound.

slide-52
SLIDE 52

The Many Meanings of Assignment Statements

7

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 of the current env.

  • 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 it is bound.

slide-53
SLIDE 53

The Many Meanings of Assignment Statements

7

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 of the current env.

  • 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 it is bound.

slide-54
SLIDE 54

The Many Meanings of Assignment Statements

7

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 of the current env.

  • 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 it is bound.

slide-55
SLIDE 55

The Many Meanings of Assignment Statements

7

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 of the current env.

  • 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 it is bound.

slide-56
SLIDE 56

Python Particulars

8

slide-57
SLIDE 57

Python Particulars

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

8

slide-58
SLIDE 58

Python Particulars

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

8

slide-59
SLIDE 59

Python Particulars

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

8

slide-60
SLIDE 60

Python Particulars

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

8

Local assignment

slide-61
SLIDE 61

Python Particulars

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

8

Local assignment

slide-62
SLIDE 62

Mutable Values & Persistent Local State

9

Mutable values can be changed without a nonlocal statement.

slide-63
SLIDE 63

Mutable Values & Persistent Local State

9

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

slide-64
SLIDE 64

Mutable Values & Persistent Local State

9

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

slide-65
SLIDE 65

Creating Two Different Withdraw Functions

10

Demo

slide-66
SLIDE 66

The Benefit of Non-Local Assignment

11

slide-67
SLIDE 67

The Benefit of Non-Local Assignment

  • Ability to maintain some state that is local to a function,

but evolves over successive calls to that function.

11

slide-68
SLIDE 68

The Benefit of Non-Local Assignment

  • Ability to maintain some state that is local to a function,

but evolves over successive calls to that function.

  • The binding for balance in the first non-local frame of the

environment associated with an instance of withdraw is inaccessible to the rest of the program.

11

slide-69
SLIDE 69

The Benefit of Non-Local Assignment

  • Ability to maintain some state that is local to a function,

but evolves over successive calls to that function.

  • The binding for balance in the first non-local frame of the

environment associated with an instance of withdraw is inaccessible to the rest of the program.

  • An abstraction of a bank account that manages its own

internal state.

11

slide-70
SLIDE 70

The Benefit of Non-Local Assignment

  • Ability to maintain some state that is local to a function,

but evolves over successive calls to that function.

  • The binding for balance in the first non-local frame of the

environment associated with an instance of withdraw is inaccessible to the rest of the program.

  • An abstraction of a bank account that manages its own

internal state.

11

John's Account $10

slide-71
SLIDE 71

The Benefit of Non-Local Assignment

  • Ability to maintain some state that is local to a function,

but evolves over successive calls to that function.

  • The binding for balance in the first non-local frame of the

environment associated with an instance of withdraw is inaccessible to the rest of the program.

  • An abstraction of a bank account that manages its own

internal state.

11

John's Account $10 Steven's Account $1,000,000

slide-72
SLIDE 72

Multiple References to a Single Withdraw Function

12

Demo

slide-73
SLIDE 73

Sameness and Change

13

slide-74
SLIDE 74

Sameness and Change

  • As long as we never modify objects, we can regard a compound object

to be precisely the totality of its pieces.

13

slide-75
SLIDE 75

Sameness and Change

  • As long as we never modify objects, we can regard a compound object

to be precisely the totality of its pieces.

  • A rational number is just its numerator and denominator.

13

slide-76
SLIDE 76

Sameness and Change

  • As long as we never modify objects, we can regard a compound object

to be precisely the totality of its pieces.

  • A rational number is just its numerator and denominator.
  • This view is no longer valid in the presence of change.

13

slide-77
SLIDE 77

Sameness and Change

  • As long as we never modify objects, we can regard a compound object

to be precisely the totality of its pieces.

  • A rational number is just its numerator and denominator.
  • This view is no longer valid in the presence of change.
  • Now, a compound data object has an "identity" that is something

more than the pieces of which it is composed.

13

slide-78
SLIDE 78

Sameness and Change

  • As long as we never modify objects, we can regard a compound object

to be precisely the totality of its pieces.

  • A rational number is just its numerator and denominator.
  • This view is no longer valid in the presence of change.
  • Now, a compound data object has an "identity" that is something

more than the pieces of which it is composed.

  • A bank account is still "the same" bank account even if we change

the balance by making a withdrawal.

13

slide-79
SLIDE 79

Sameness and Change

  • As long as we never modify objects, we can regard a compound object

to be precisely the totality of its pieces.

  • A rational number is just its numerator and denominator.
  • This view is no longer valid in the presence of change.
  • Now, a compound data object has an "identity" that is something

more than the pieces of which it is composed.

  • A bank account is still "the same" bank account even if we change

the balance by making a withdrawal.

  • Conversely, we could have two bank accounts that happen to have the

same balance, but are different objects.

13

slide-80
SLIDE 80

Sameness and Change

  • As long as we never modify objects, we can regard a compound object

to be precisely the totality of its pieces.

  • A rational number is just its numerator and denominator.
  • This view is no longer valid in the presence of change.
  • Now, a compound data object has an "identity" that is something

more than the pieces of which it is composed.

  • A bank account is still "the same" bank account even if we change

the balance by making a withdrawal.

  • Conversely, we could have two bank accounts that happen to have the

same balance, but are different objects.

13

John's Account $10

slide-81
SLIDE 81

Sameness and Change

  • As long as we never modify objects, we can regard a compound object

to be precisely the totality of its pieces.

  • A rational number is just its numerator and denominator.
  • This view is no longer valid in the presence of change.
  • Now, a compound data object has an "identity" that is something

more than the pieces of which it is composed.

  • A bank account is still "the same" bank account even if we change

the balance by making a withdrawal.

  • Conversely, we could have two bank accounts that happen to have the

same balance, but are different objects.

13

John's Account $10 Steven's Account $10

slide-82
SLIDE 82

Referential Transparency, Lost

14

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.

14

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.

14

mul(add(2, mul(4, 6)), add(3, 5))

slide-85
SLIDE 85

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an

expression with its value does not change the meaning of a program.

14

mul(add(2, mul(4, 6)), add(3, 5)) mul(add(2, 24 ), add(3, 5))

slide-86
SLIDE 86

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an

expression with its value does not change the meaning of a program.

14

mul(add(2, mul(4, 6)), add(3, 5)) mul(add(2, 24 ), add(3, 5)) mul( 26 , add(3, 5))

slide-87
SLIDE 87

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an

expression with its value does not change the meaning of a program.

14

  • Re-binding operations violate the condition of referential

transparency because they let us define functions that do more than just return a value; we can change the environment, causing values to mutate. mul(add(2, mul(4, 6)), add(3, 5)) mul(add(2, 24 ), add(3, 5)) mul( 26 , add(3, 5))

slide-88
SLIDE 88

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an

expression with its value does not change the meaning of a program.

14

  • Re-binding operations violate the condition of referential

transparency because they let us define functions that do more than just return a value; we can change the environment, causing values to mutate. mul(add(2, mul(4, 6)), add(3, 5)) mul(add(2, 24 ), add(3, 5)) mul( 26 , add(3, 5))

slide-89
SLIDE 89

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an

expression with its value does not change the meaning of a program.

14

  • Re-binding operations violate the condition of referential

transparency because they let us define functions that do more than just return a value; we can change the environment, causing values to mutate. mul(add(2, mul(4, 6)), add(3, 5)) mul(add(2, 24 ), add(3, 5)) mul( 26 , add(3, 5))

slide-90
SLIDE 90

Referential Transparency, Lost

  • Expressions are referentially transparent if substituting an

expression with its value does not change the meaning of a program.

14

  • Re-binding operations violate the condition of referential

transparency because they let us define functions that do more than just return a value; we can change the environment, causing values to mutate. mul(add(2, mul(4, 6)), add(3, 5)) mul(add(2, 24 ), add(3, 5)) mul( 26 , add(3, 5)) Demo