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 of $100 >>> withdraw(25) 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 >>> withdraw(25) 75 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: >>> withdraw(25) amount to withdraw 75 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: >>> withdraw(25) amount to withdraw remaining balance 75 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: >>> withdraw(25) amount to withdraw remaining balance 75 >>> withdraw(25) 50 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: >>> withdraw(25) amount to withdraw remaining balance 75 >>> withdraw(25) Second withdrawal of 50 the same amount 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: >>> withdraw(25) amount to withdraw remaining balance 75 >>> withdraw(25) Second withdrawal of 50 the same amount Different return value! 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: >>> withdraw(25) amount to withdraw remaining balance 75 >>> withdraw(25) Second withdrawal of 50 the same amount Different return value! >>> withdraw(60) 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: >>> withdraw(25) amount to withdraw remaining balance 75 >>> withdraw(25) Second withdrawal of 50 the same amount Different return value! >>> withdraw(60) 'Insufficient funds' 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: >>> withdraw(25) amount to withdraw remaining balance 75 >>> withdraw(25) Second withdrawal of 50 the same amount Different return value! >>> withdraw(60) 'Insufficient funds' Where's this balance stored? 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: >>> withdraw(25) amount to withdraw remaining balance 75 >>> withdraw(25) Second withdrawal of 50 the same amount Different return value! >>> withdraw(60) 'Insufficient funds' Where's this balance stored? >>> withdraw = make_withdraw(100) 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: >>> withdraw(25) amount to withdraw remaining balance 75 >>> withdraw(25) Second withdrawal of 50 the same amount Different return value! >>> withdraw(60) 'Insufficient funds' Where's this balance stored? >>> withdraw = make_withdraw(100) Within the parent frame of the function! 4
A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 Argument: Return value: >>> withdraw(25) amount to withdraw remaining balance 75 >>> withdraw(25) Second withdrawal of 50 the same amount Different return value! >>> withdraw(60) 'Insufficient funds' Where's this balance stored? >>> withdraw = make_withdraw(100) Within the parent frame A function has a body and of the function! a parent environment 4
Persistent Local State Using Environments 5 Interactive Diagram
Persistent Local State Using Environments The parent frame contains the balance, the local state of the withdraw function 5 Interactive Diagram
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
Persistent Local State Using Environments The parent frame contains the balance, the local state of the withdraw function All calls to the Every call decreases the same balance same function by (a possibly different) amount have the same parent 5 Interactive Diagram
Reminder: Local Assignment 6 Interactive Diagram
Reminder: Local Assignment Assignment binds name(s) to value(s) in the first frame of the current environment 6 Interactive Diagram
Reminder: Local Assignment Assignment binds name(s) to value(s) in the first frame of the current environment 6 Interactive Diagram
Reminder: Local Assignment Assignment binds name(s) to value(s) in the first frame of the current environment Execution rule for assignment statements: 6 Interactive Diagram
Reminder: Local Assignment Assignment binds name(s) to value(s) in the first frame of the current environment 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 6 Interactive Diagram
Non-Local Assignment & Persistent Local State 7
Non-Local Assignment & Persistent Local State def make_withdraw(balance): 7
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" 7
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): 7
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance 7
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
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
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
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
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
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 at the top of nonlocal balance the body of the function in which it is re-assigned if amount > balance: return 'Insufficient funds' balance = balance - amount return balance return withdraw 7
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 at the top of nonlocal balance the body of the function in which it is re-assigned if amount > balance: return 'Insufficient funds' balance = balance - amount Re-bind balance in the first non-local frame in which it was bound previously return balance return withdraw 7
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 at the top of nonlocal balance the body of the function in which it is re-assigned if amount > balance: return 'Insufficient funds' balance = balance - amount Re-bind balance in the first non-local frame in which it was bound previously return balance return withdraw (Demo) 7
Non-Local Assignment
The Effect of Nonlocal Statements nonlocal <name> 9
Recommend
More recommend