DOC PREVIEW
Berkeley COMPSCI 61A - Lecture 11

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

61A Lecture 11Friday, September 23A Function with Behavior That Varies Over Time2>>> 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 $100Argument: amount to withdrawSecond withdrawal of the same amountReturn value:remaining balanceDifferentreturn value!Where's this balance stored?Within the function!Persistent Local State3withdraw(amount):function body to be revealedmomentarilybalance: 100withdraw:make_withdraw:withdraw:make_withdraw(balance):function body to be revealedmomentarilyLocal State via Non-Local Assignment4 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 withdrawDeclare the name "balance" nonlocalRe-bind the existing balance name aboveDemoLocal, Non-Local, and Global Frames5Non-local frameThe global frameNon-local frameAn environmentLocal frameFirst frameSecond frameFirst non-local frameThe Effect of Nonlocal Statements6http://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-statementEffect: 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"The Many Meanings of Assignment Statements7x = 2Status Effect•No nonlocal statement•"x" is not bound locallyCreate a new binding from name "x" to object 2 in the first frame of the current environment.•No nonlocal statement•"x" is bound locallyRe-bind name "x" to object 2 in the first frame of the current env.•nonlocal x•"x" is bound locallySyntaxError: name 'x' is parameter and nonlocal•nonlocal x•"x" is not bound in a non-local frameSyntaxError: no binding for nonlocal 'x' found•nonlocal x•"x" is bound in a non-local frameRe-binds "x" to 2 in the first non-local frame of the current environment in which that name is already bound.turtlemutantmutant(y)y, x = y+1, y+2return ninja(y)/2ninja(y)Assignment Review: Teenage Mutant Ninja Turtles8def mutant(y): y, x = y+1, y+2 return ninja(y)/2def ninja(x): return x + 2def turtle(x): return x * y + 2y, ninja = 5, turtlemutant(y)y: 5ninja:turtle:mutant:return x * y + 2x: 63216x: 76mutant(y):y, x = y+1, y+2return ninja(y)/2ninja(x):return x + 2turtle(x)return x * y + 2y: 5Assignment Review: Teenage Mutant Ninja Turtles9def mutant(y): y, x = y+1, y+2 return ninja(y)/2def ninja(x): return x + 2def turtle(x): return x * y + 2y, ninja = 5, turtlemutant(y)•Bind mutant, ninja, and turtle to their respective functions•Simultaneously: bind y to 5 and ninja to the turtle function•Apply the mutant function to 5•In the first frame, bind y to 6 and x to 7•Look up ninja, which is bound to the turtle function•Look up y, which is bound to 6•Apply the turtle function to 6•Look up x, which is bound to 6in the local frame•Look up y, which is bound to 5in the global frame•Return 32•Return half the result: 16Intrinsic function namemake_withdrawmake_withdraw:withdraw(amount):...balance:make_withdrawwithdraw:wd:wd(5)20Environment Diagram of Withdraw1015wd = make_withdraw(20)wd(5)15nonlocal balance if amount > balance: return 'Insufficient funds'balance = balance - amountreturn balanceamount: 5withdrawbalance:Calling a Withdraw Function Twice11make_withdrawmake_withdraw:wd = make_withdraw(20)wd(5)wd(3)amount: 5withdrawwd(5)12nonlocal balance if amount > balance: return 'Insufficient funds'balance = balance - amountreturn balanceamount: 3withdrawwithdraw(amount):...balance:make_withdrawwithdraw:wd:15 12balance:Creating Two Different Withdraw Functions12withdraw(amount):make_withdrawmake_withdraw:balance:make_withdraw12withdraw:wd:wd = make_withdraw(20)wd(5)wd(3)wd2 = make_withdraw(7)wd2(6)make_withdraw(7) def withdraw(amount): ... return withdrawwd2:balance: 7make_withdrawwithdraw(amount):withdraw:Creating Two Different Withdraw Functions13withdraw(amount):make_withdrawmake_withdraw:balance:make_withdraw12withdraw:wd:wd = make_withdraw(20)wd(5)wd(3)wd2 = make_withdraw(7)wd2(6)wd2:withdraw(amount):balance:make_withdraw7withdraw:wd2(6)1amount: 6withdraw1nonlocal balance if amount > balance: return 'Insufficient funds'balance = balance - amountreturn balancebalance: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.14John's Account$10Steven's Account$1,000,000Multiple References to a Single Withdraw Function15withdraw(amount):make_withdrawmake_withdraw:balance:make_withdrawwithdraw:wd:wd = make_withdraw(12)wd2 = wdwd2(1)wd(1)wd2:amount: 1withdraw1112wd2(1)nonlocal balance if amount > balance: return 'Insufficient funds'balance = balance - amountreturn balance11Multiple References to a Single Withdraw Function16withdraw(amount):make_withdrawmake_withdraw:balance:make_withdraw10withdraw:wd:wd = make_withdraw(12)wd2 = wdwd2(1)wd(1)wd2:amount: 1withdrawamount: 1withdraw1112wd(1)nonlocal balance if amount > balance: return 'Insufficient funds'balance = balance - amountreturn balance10Sameness and Change• So long as we never modify data objects, we can regard a compound object to be precisely the totality of its pieces.• A rational number is determined by 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.17John's


View Full Document

Berkeley COMPSCI 61A - Lecture 11

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Lecture 11
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 11 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 11 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?