61a lecture 14 announcements mutable functions a function
play

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


  1. 61A Lecture 14

  2. Announcements

  3. Mutable Functions

  4. A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 4

  5. A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 >>> withdraw(25) 4

  6. A Function with Behavior That Varies Over Time Let's model a bank account that has a balance of $100 >>> withdraw(25) 75 4

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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Persistent Local State Using Environments 5 Interactive Diagram

  19. Persistent Local State Using Environments The parent frame contains the balance, the local state of the withdraw function 5 Interactive Diagram

  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

  21. 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

  22. Reminder: Local Assignment 6 Interactive Diagram

  23. Reminder: Local Assignment Assignment binds name(s) to value(s) in the first frame of the current environment 6 Interactive Diagram

  24. Reminder: Local Assignment Assignment binds name(s) to value(s) in the first frame of the current environment 6 Interactive Diagram

  25. 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

  26. 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

  27. Non-Local Assignment & Persistent Local State 7

  28. Non-Local Assignment & Persistent Local State def make_withdraw(balance): 7

  29. Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" 7

  30. Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): 7

  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

  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

  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

  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

  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

  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

  37. 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

  38. 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

  39. 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

  40. Non-Local Assignment

  41. The Effect of Nonlocal Statements nonlocal <name> 9

Recommend


More recommend