SLIDE 1
61A Lecture 14 Announcements Mutable Functions A Function with - - PowerPoint PPT Presentation
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 2
SLIDE 3
Mutable Functions
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Persistent Local State Using Environments
5
Interactive Diagram
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
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
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
Reminder: Local Assignment
6
Interactive Diagram
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
Reminder: Local Assignment
Assignment binds name(s) to value(s) in the first frame of the current environment
6
Interactive Diagram
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
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
Non-Local Assignment & Persistent Local State
7
SLIDE 28
Non-Local Assignment & Persistent Local State
def make_withdraw(balance):
7
SLIDE 29
Non-Local Assignment & Persistent Local State
def make_withdraw(balance): """Return a withdraw function with a starting balance."""
7
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
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
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
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
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
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
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
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
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
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
Non-Local Assignment
SLIDE 41
The Effect of Nonlocal Statements
nonlocal <name>
9
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
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
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
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
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
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
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
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
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
The Many Meanings of Assignment Statements
x = 2
10
SLIDE 52
The Many Meanings of Assignment Statements
x = 2 Status Effect
10
SLIDE 53
The Many Meanings of Assignment Statements
x = 2 Status Effect
- No nonlocal statement
- "x" is not bound locally
10
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
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
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
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
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
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
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
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
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
Python Particulars
11
SLIDE 64
Python Particulars
Python pre-computes which frame contains each name before executing the body of a function.
11
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
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
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
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
Mutable Values & Persistent Local State
Mutable values can be changed without a nonlocal statement.
12
SLIDE 70
Mutable Values & Persistent Local State
Mutable values can be changed without a nonlocal statement.
12
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
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
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
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
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
Multiple Mutable Functions
(Demo)
SLIDE 77
Referential Transparency, Lost
14
Interactive Diagram
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
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
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
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
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
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
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