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 Argument: amount to withdraw >>> withdraw(25) 2
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: amount to withdraw >>> withdraw(25) remaining balance 75 2
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: amount to withdraw >>> withdraw(25) remaining balance 75 >>> withdraw(25) Second withdrawal of the same amount 2
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: amount to withdraw >>> withdraw(25) remaining balance 75 >>> withdraw(25) Second withdrawal 50 Different of the same amount return value! 2
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: amount to withdraw >>> withdraw(25) remaining balance 75 >>> withdraw(25) Second withdrawal 50 Different of the same amount return value! >>> withdraw(60) 2
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: amount to withdraw >>> withdraw(25) remaining balance 75 >>> withdraw(25) Second withdrawal 50 Different of the same amount return value! >>> withdraw(60) 'Insufficient funds' 2
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: amount to withdraw >>> withdraw(25) remaining balance 75 >>> withdraw(25) Second withdrawal 50 Different of the same amount return value! >>> withdraw(60) 'Insufficient funds' >>> withdraw(15) 2
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: amount to withdraw >>> withdraw(25) remaining balance 75 >>> withdraw(25) Second withdrawal 50 Different of the same amount return value! >>> withdraw(60) 'Insufficient funds' >>> withdraw(15) 35 2
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: amount to withdraw >>> withdraw(25) remaining balance 75 >>> withdraw(25) Second withdrawal 50 Different of the same amount return value! >>> withdraw(60) 'Insufficient funds' >>> withdraw(15) Where's this 35 balance stored? 2
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: amount to withdraw >>> withdraw(25) remaining balance 75 >>> withdraw(25) Second withdrawal 50 Different of the same amount return value! >>> withdraw(60) 'Insufficient funds' >>> withdraw(15) Where's this 35 balance stored? >>> withdraw = make_withdraw(100) 2
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: amount to withdraw >>> withdraw(25) remaining balance 75 >>> withdraw(25) Second withdrawal 50 Different of the same amount return value! >>> withdraw(60) 'Insufficient funds' >>> withdraw(15) Where's this 35 balance stored? >>> withdraw = make_withdraw(100) Within the function! 2
Persistent Local State http://goo.gl/StRZP 3
Persistent Local State A function with a parent frame http://goo.gl/StRZP 3
Persistent Local State A function with a parent frame The parent contains local state http://goo.gl/StRZP 3
Persistent Local State A function with a parent frame The parent contains local state Every call changes the balance http://goo.gl/StRZP 3
Reminder: Local Assignment 4 Example: http://goo.gl/wcF71
Reminder: Local Assignment Assignment binds names to values in the current local frame 4 Example: http://goo.gl/wcF71
Reminder: Local Assignment Assignment binds names to values in the current local frame Execution rule for assignment statements: 4 Example: http://goo.gl/wcF71
Reminder: Local Assignment Assignment binds names to values in the current local frame 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. 4 Example: http://goo.gl/wcF71
Non-Local Assignment & Persistent Local State 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): Declare the name "balance" nonlocal nonlocal balance 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): Declare the name "balance" nonlocal nonlocal balance if amount > balance: 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): Declare the name "balance" nonlocal nonlocal balance if amount > balance: return 'Insufficient funds' 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): Declare the name "balance" nonlocal nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): Declare the name "balance" nonlocal nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount Re-bind balance where it was bound previously 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): Declare the name "balance" nonlocal nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount Re-bind balance where it was bound previously return balance 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): Declare the name "balance" nonlocal nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount Re-bind balance where it was bound previously return balance return withdraw 5
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): Declare the name "balance" nonlocal nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount Re-bind balance where it was bound previously return balance return withdraw Demo 5
The Effect of Nonlocal Statements nonlocal <name> 6
The Effect of Nonlocal Statements nonlocal <name> 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. 6
The Effect of Nonlocal Statements nonlocal <name> 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. Python Docs: an "enclosing scope" 6
The Effect of Nonlocal Statements nonlocal <name> , <name 2>, ... 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. Python Docs: an "enclosing scope" 6
Recommend
More recommend