cs 61a discussion 6
play

CS 61A Discussion 6 Nonlocal and Object Oriented Programming Albert - PowerPoint PPT Presentation

CS 61A Discussion 6 Nonlocal and Object Oriented Programming Albert Xu Slides: albertxu.xyz/teaching/cs61a/ Announcements Nonlocal changing some of your programming paradigms remember how we said that each frame has its own set of


  1. CS 61A Discussion 6 Nonlocal and Object Oriented Programming Albert Xu Slides: albertxu.xyz/teaching/cs61a/

  2. Announcements

  3. Nonlocal changing some of your programming paradigms • remember how we said that each frame has its own set of variables?

  4. Nonlocal changing some of your programming paradigms • remember how we said that each frame has its own set of variables? • if a variable doesn’t exist in the current frame(say, f2), you can look up its value from a parent frame(say, f1)

  5. Nonlocal changing some of your programming paradigms • remember how we said that each frame has its own set of variables? • if a variable doesn’t exist in the current frame(say, f2), you can look up its value from a parent frame(say, f1) • …but it’s impossible for you to change the value of that parent frame’s(f1’s) variable from inside f2!

  6. Nonlocal changing some of your programming paradigms • remember how we said that each frame has its own set of variables? • if a variable doesn’t exist in the current frame(say, f2), you can look up its value from a parent frame(say, f1) • …but it’s impossible for you to change the value of that parent frame’s(f1’s) variable from inside f2! not anymore!

  7. Nonlocal changing some of your programming paradigms • remember how we said that each frame has its own set of variables? • if a variable doesn’t exist in the current frame(say, f2), you can look up its value from a parent frame(say, f1) • …but it’s impossible for you to change the value of that parent frame’s(f1’s) variable from inside f2! not anymore! nonlocal x

  8. Nonlocal changing some of your programming paradigms • remember how we said that each frame has its own set of variables? • if a variable doesn’t exist in the current frame(say, f2), you can look up its value from a parent frame(say, f1) • …but it’s impossible for you to change the value of that parent frame’s(f1’s) variable from inside f2! not anymore! nonlocal x this line, when run inside f2, says that every time we modify x inside the current frame(f2), instead modify f1’s x! Same thing with looking up x.

  9. Nonlocal Demo demo!

  10. Pitfalls of Nonlocal there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

  11. Pitfalls of Nonlocal there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well Exhibit A

  12. Pitfalls of Nonlocal there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well attempting to nonlocal a variable Exhibit A that already exists in the current frame

  13. Pitfalls of Nonlocal there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well I L L E G A L ! attempting to ! ! nonlocal a variable Exhibit A that already exists in the current frame

  14. Pitfalls of Nonlocal there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well I L L E G A L ! attempting to ! ! nonlocal a variable Exhibit A that already exists in the current frame Exhibit B

  15. Pitfalls of Nonlocal there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well I L L E G A L ! attempting to ! ! nonlocal a variable Exhibit A that already exists in the current frame attempting to Exhibit B nonlocal a variable from the global frame

  16. Pitfalls of Nonlocal there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well I L L E G A L ! attempting to ! ! nonlocal a variable Exhibit A that already exists in the current frame attempting to Exhibit B nonlocal a variable from the global frame ILLEGAL!!!

  17. UnboundLocalError this is probably a good time to talk about it *i borrowed this incorrect code from StackOverflow lol

  18. UnboundLocalError this is probably a good time to talk about it here is code which throws an UnboundLocalError *i borrowed this incorrect code from StackOverflow lol

  19. UnboundLocalError this is probably a good time to talk about it here is code which throws an UnboundLocalError …which is equivalent to *i borrowed this incorrect code from StackOverflow lol

  20. UnboundLocalError this is probably a good time to talk about it here is code which throws an UnboundLocalError …which is equivalent to Python has no variable declarations, so it has to guess the scope of your variables! Because you assigned a value of x inside the increment function, it assumes that x is a variable inside the increment frame. But by the order in which we evaluate the RHS of assignment before the left, we look up x before it even exists in the frame! *i borrowed this incorrect code from StackOverflow lol

  21. UnboundLocalError this is probably a good time to talk about it here is code which throws an UnboundLocalError …which is equivalent to Python has no variable declarations, so it has to guess the scope of your variables! Because you assigned a value of x inside the increment function, it assumes that x is a variable inside the increment frame. this is an analysis of the issue, but it’s not the root cause. How could But by the order in which we evaluate the RHS of anonymous SO user easily fix this? assignment before the left, we look up x before it even exists in the frame! *i borrowed this incorrect code from StackOverflow lol

  22. UnboundLocalError this is probably a good time to talk about it here is code which throws an UnboundLocalError …which is equivalent to Python has no variable declarations, so it has to guess the scope of your variables! Because you assigned a value of x inside the increment function, it assumes that x is a variable inside the increment frame. this is an analysis of the issue, but it’s not the root cause. How could But by the order in which we evaluate the RHS of anonymous SO user easily fix this? assignment before the left, we look up x before it even trick question!! exists in the frame! *i borrowed this incorrect code from StackOverflow lol

  23. UnboundLocalError this is probably a good time to talk about it code that errors. *i borrowed this incorrect code from StackOverflow lol

  24. UnboundLocalError this is probably a good time to talk about it code that errors. *i borrowed this incorrect code from StackOverflow lol

  25. UnboundLocalError this is probably a good time to talk about it code that errors. Also errors!! remember? ILLEGAL!!! *i borrowed this incorrect code from StackOverflow lol

  26. UnboundLocalError this is probably a good time to talk about it code that errors. Also errors!! remember? ILLEGAL!!! this one works! *i borrowed this incorrect code from StackOverflow lol

  27. UnboundLocalError this is probably a good time to talk about it code that errors. Also errors!! remember? ILLEGAL!!! this one works! unfortunately you don’t necessarily learn the global keyword in this class, just fyi! *i borrowed this incorrect code from StackOverflow lol

  28. First, A Meme but it’s okay! you probably won’t need to deal with this until 61B.

  29. Next, Data Abstraction How would you write an ADT for a Dog ? What’s the general ADT form?

  30. Next, Data Abstraction How would you write an ADT for a Student ? What’s the general ADT form? def student(<attr0>, <attr1>, <attr2> ...): constructor return [<attr0>, <attr1>, <attr2> ...]

  31. Next, Data Abstraction How would you write an ADT for a Student ? What’s the general ADT form? def student(<attr0>, <attr1>, <attr2> ...): constructor return [<attr0>, <attr1>, <attr2> ...] def attr0(student): return student[0] selectors def attr1(student): return student[1] ...

  32. Next, Data Abstraction How would you write an ADT for a Student ? What’s the general ADT form? def student(<attr0>, <attr1>, <attr2> ...): constructor return [<attr0>, <attr1>, <attr2> ...] def attr0(student): return student[0] selectors def attr1(student): return student[1] ... Let’s fill in these with some Student attributes.

  33. Next, Data Abstraction Here’s our completed ADT for a Student! def student(name, ta): constructor return [name, ta, 0] def name(student): return student[0] def ta(student): return student[1] selectors def understanding(student): return student[2] def course(student): return “CS 61A”

  34. Next, Data Abstraction Here’s our completed ADT for a Student! def student(name, ta): constructor return [name, ta, 0] def name(student): return student[0] def ta(student): return student[1] selectors def understanding(student): return student[2] def course(student): return “CS 61A” Some Vocab: • attributes : traits of a data type or object • instance : a specific data type made by the constructor • class attributes : traits that all instances share (scientific name) • instance attributes : traits that di ff er between instances (age)

  35. OOP Formal Definitions We’ll need to know these words to write Student • Class - a “template” for an object • represents attributes of a student, and the actions( methods ) it can take

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend