61a lecture 13

61A Lecture 13 Wednesday, September 26 A Function with Behavior - PowerPoint PPT Presentation

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


  1. 61A Lecture 13 Wednesday, September 26

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

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

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

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

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

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

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

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

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

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

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

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

  14. Persistent Local State http://goo.gl/StRZP 3

  15. Persistent Local State A function with a parent frame http://goo.gl/StRZP 3

  16. Persistent Local State A function with a parent frame The parent contains local state http://goo.gl/StRZP 3

  17. Persistent Local State A function with a parent frame The parent contains local state Every call changes the balance http://goo.gl/StRZP 3

  18. Reminder: Local Assignment 4 Example: http://goo.gl/wcF71

  19. Reminder: Local Assignment Assignment binds names to values in the current local frame 4 Example: http://goo.gl/wcF71

  20. Reminder: Local Assignment Assignment binds names to values in the current local frame Execution rule for assignment statements: 4 Example: http://goo.gl/wcF71

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

  22. Non-Local Assignment & Persistent Local State 5

  23. Non-Local Assignment & Persistent Local State def make_withdraw(balance): 5

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

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

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

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

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

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

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

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

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

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

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

  35. The Effect of Nonlocal Statements nonlocal <name> 6

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

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

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