DOC PREVIEW
Berkeley COMPSCI 61A - Lecture 11

This preview shows page 1-2-3-4-5-6-7-8-9-10-11-12-80-81-82-83-84-85-86-87-88-89-90-162-163-164-165-166-167-168-169-170-171-172-173 out of 173 pages.

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

Unformatted text preview:

61A Lecture 11Friday, September 23Friday, September 23, 2011A Function with Behavior That Varies Over Time2Let's model a bank account that has a balance of $100Friday, September 23, 2011A Function with Behavior That Varies Over Time2>>> withdraw(25)Let's model a bank account that has a balance of $100Argument:amount to withdrawFriday, September 23, 2011A Function with Behavior That Varies Over Time2>>> withdraw(25)75Let's model a bank account that has a balance of $100Argument:amount to withdrawReturn value:remaining balanceFriday, September 23, 2011A Function with Behavior That Varies Over Time2>>> withdraw(25)75>>> withdraw(25)Let's model a bank account that has a balance of $100Argument:amount to withdrawSecond withdrawal of the same amountReturn value:remaining balanceFriday, September 23, 2011A Function with Behavior That Varies Over Time2>>> withdraw(25)75>>> withdraw(25)50Let's model a bank account that has a balance of $100Argument:amount to withdrawSecond withdrawal of the same amountReturn value:remaining balanceDifferentreturn value!Friday, September 23, 2011A Function with Behavior That Varies Over Time2>>> withdraw(25)75>>> withdraw(25)50>>> withdraw(60)Let's model a bank account that has a balance of $100Argument:amount to withdrawSecond withdrawal of the same amountReturn value:remaining balanceDifferentreturn value!Friday, September 23, 2011A Function with Behavior That Varies Over Time2>>> withdraw(25)75>>> withdraw(25)50>>> withdraw(60)'Insufficient funds'Let's model a bank account that has a balance of $100Argument:amount to withdrawSecond withdrawal of the same amountReturn value:remaining balanceDifferentreturn value!Friday, September 23, 2011A Function with Behavior That Varies Over Time2>>> withdraw(25)75>>> withdraw(25)50>>> withdraw(60)'Insufficient funds'>>> withdraw(15)Let's model a bank account that has a balance of $100Argument:amount to withdrawSecond withdrawal of the same amountReturn value:remaining balanceDifferentreturn value!Friday, September 23, 2011A Function with Behavior That Varies Over Time2>>> withdraw(25)75>>> withdraw(25)50>>> withdraw(60)'Insufficient funds'>>> withdraw(15)35Let's model a bank account that has a balance of $100Argument:amount to withdrawSecond withdrawal of the same amountReturn value:remaining balanceDifferentreturn value!Friday, September 23, 2011A Function with Behavior That Varies Over Time2>>> withdraw(25)75>>> withdraw(25)50>>> withdraw(60)'Insufficient funds'>>> withdraw(15)35Let'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?Friday, September 23, 2011A 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?Friday, September 23, 2011A 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!Friday, September 23, 2011Persistent Local State3Friday, September 23, 2011Persistent Local State3withdraw(amount):function body to be revealedmomentarilyFriday, September 23, 2011Persistent Local State3withdraw(amount):function body to be revealedmomentarilybalance: 100withdraw:make_withdrawFriday, September 23, 2011Persistent Local State3withdraw(amount):function body to be revealedmomentarilymake_withdraw:withdraw:make_withdraw(balance):function body to be revealedmomentarilybalance: 100withdraw:make_withdrawFriday, September 23, 2011Local State via Non-Local Assignment4Friday, September 23, 2011Local State via Non-Local Assignment4 def make_withdraw(balance):Friday, September 23, 2011Local State via Non-Local Assignment4 def make_withdraw(balance): """Return a withdraw function with a starting balance."""Friday, September 23, 2011Local State via Non-Local Assignment4 def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount):Friday, September 23, 2011Local State via Non-Local Assignment4 def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance Friday, September 23, 2011Local State via Non-Local Assignment4 def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance Declare the name "balance" nonlocalFriday, September 23, 2011Local 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:Declare the name "balance" nonlocalFriday, September 23, 2011Local 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'Declare the name "balance" nonlocalFriday, September 23, 2011Local 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 - amountDeclare the name "balance" nonlocalFriday, September 23, 2011Local 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 - amountDeclare the name "balance" nonlocalRe-bind balance where it was bound previouslyFriday, September 23, 2011Local 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 balanceDeclare the name "balance" nonlocalRe-bind balance where it was bound


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 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?