English subtitles

← Office Hours 2 - Design of Computer Programs

Get Embed Code
1 Language

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Hi. Welcome to the second office hours.
  2. Again we had a lot of really good questions in the forum, so let's get started.
  3. The first question came from Matthew Atkinson.
  4. He has heard that you shouldn't use functions like eval and exec,
  5. which we did use in unit 2.
  6. He wants to know why shouldn't we be using these functions,
  7. and what's the problem with them.
  8. That's a great question. There's a couple reason to avoid these.
  9. On is for security, especially if you're going to be evaling
  10. a string that somebody passes in to you, you don't know what it's going to be doing.
  11. There are lots of dangerous commands that Python can execute like "delete all my files."
  12. You don't want to be asking somebody to type something in and then go execute that
  13. and allow them to do something dangerous. That's one reason.
  14. The other is just in terms of program structure.
  15. We like to write our programs so that the functions and variables and so on,
  16. have a set scope.
  17. This variable exists from here to here,
  18. and eval kind of breaks all that structure
  19. and goes outside of it and say now we're executing something right here,
  20. but it's not executing with respect to where we are in the code.
  21. It's executing someplace complete different. That makes it harder to follow the code.
  22. There's nothing wrong in terms of do you get the right answer but harder to understand.
  23. So we should minimize it.
  24. I used it in the assignment for week 2, because there was no other way to easily get around it.
  25. But you should minimize the use.
  26. Thanks. The next question comes from Sasheen.
  27. Sasheen want's to know a little bit more about this big-O notation
  28. that we introduced and saw a little bit of in Unit 2.
  29. Yeah. The answer to that question is a whole course in algorithms.
  30. We can't do that right here.
  31. But basically the idea is big O says approximately how many instructions
  32. is it going to take to execute this.
  33. Actually, big O is an upper bound on that.
  34. There's another notation less widely used with big theta which gives a more exact bound,
  35. but the idea is if I have a doubly nested loop that says for i = 1-n
  36. and then inside of that is nest for j = 1-n then that's the order n-squared,
  37. because we're going to n * n executions of the inner loop.
  38. The basic idea is that's more than if we were just doing what are n executions of the loop.
  39. So if you can get away with a single loop instead of a double loop, that's better.
  40. That's really all you need to know about big O.
  41. Alright. I should say there will be an algorithms class sometime in the near future
  42. for those of you who want to learn more about this.
  43. Thomas Grace had a question about these attributes you use--function attributes--
  44. specifically in the C function that had c.starts and c.items. What's going on there?
  45. So Dave Evans in CS101 chose not to really focus on Python classes,
  46. which is probably a good choice, because it's a big topic.
  47. The idea here is that classes of objects can have attributes.
  48. These are parts or components of the object,
  49. and they're named with this dot-notation--object, dot, then the name of a field
  50. that's a component of that object.
  51. In most languages you define all the fields ahead of time.
  52. When you define what a class looks like, you say these are the fields that compose this object.
  53. Python is a more dynamic language, and some classes of objects you're allowed to add
  54. fields to them as you go, and functions are one of those types of dynamic objects
  55. where you can at any you say I want to add a new attribute to this function.
  56. Other objects have that same approach where you can add to it.
  57. And so what was going on in the C function is that I needed two variables
  58. that I wanted to keep track of something. I needed two counters.
  59. I could have just used a global variable name, but that would be cluttering up the name space.
  60. There'd be no way for you to, if I just made up a name like cstarts and citems--
  61. by looking at the name of the variable you could figure out that they were related--
  62. but there'd be no structural way to say that these are all related. They belong together.
  63. You want to minimize the number of global variables, because people's memories are small.
  64. By putting everything into one object, we say this belongs together.
  65. Using c.start rather than a variable named cstart is a way of structuring things together.
  66. In Python you can do that automatically. You don't have to have a declaration.
  67. You can just say I want to add a new attribute to a function, and you go ahead and assign it.
  68. Great. I had no idea you could do that either. I was very excited to see that.
  69. Yeah. If you come from a language like Java or C, you're not used to that.
  70. There you've got to declare ahead of time what your attributes are going to be.
  71. Right. Next question comes from Tracy Zelman,
  72. and she just wants to hear a little bit about when do we use lambdas.
  73. How do you decide when to use a normal function versus lambda.
  74. All a lambda is is a function definition without assigning a name to it.
  75. In a normal function definition we say "def", then we say the function name,
  76. the arguments, and the body of the function.
  77. We're simultaneously doing two things.
  78. We're defining what the function does, and we're giving it a name.
  79. Now, what a lambda does is break those out
  80. to say we're defining a function, but we're not giving it a name.
  81. Why would you want to do that?
  82. Well, we do it all the time.
  83. If we have an expression--like say we have the arithmetic expression x + 1 * x - 1,
  84. we didn't both to give names to those subcomponents--the x + 1 and the x - 1.
  85. We just wrote them down. So a lambda is like that.
  86. It's a function that's a small thing. We didn't want to bother giving it a name.
  87. We just wanted to use it in place.
  88. So that's the advantage of lambda that you aren't bothered with a name,
  89. and you can just write it right there without having to define it at one point
  90. and then use it at a second point. That's the good thing about it.
  91. The bad thing about it is that it's limited.
  92. In Python lambda can only have an expression.
  93. It can't have statements in it, and it just doesn't really fit with the rest of the language.
  94. Some languages are really designed around expressions like that, and they go really well.
  95. In Python it doesn't fit that much.
  96. I think mostly you should avoid it and just define a function with its name in the normal way,
  97. but sometimes it's really convenient to put everything in one place
  98. rather than having to define in one place and use in a second place.
  99. Great. Udacer had some questions about the program design strategy that we've been using.
  100. Namely, should we implement the strategy that we've been using
  101. in every single problem we approach? Is there somewhere we don't really need it?
  102. If we chose not to use the strategy, what are some of the disadvantages?
  103. Well, whatever you can do to get your job done, go ahead and do it.
  104. I've given you some approaches.
  105. I wouldn't say that there is just one strategy, but rather there is a collection of strategies
  106. that you want to be able to understand what's going on,
  107. make your inventory of concepts,
  108. build those up. You have a lot of choices within that.
  109. Do I want to start top down, bottom up, middle out? That's all up to you.
  110. As long as you arrive at the end of a description of the whole domain,
  111. and the whole program and you get it right, then congratulations to you.
  112. I think every body develops their own style for how they go about addressing this,
  113. but there are ideas of what it means to have a high-quality program--
  114. correctness, efficiency, and so on.
  115. Then more driven by taste is what does it mean to be readable and extensible.
  116. You'll learn that over time.
  117. Great. For one last question, Takanzi wants to know if you have any good recommendations
  118. for computer science, and specifically Python, books.
  119. Oh, books. Okay.
  120. Yes, I do have some recommendations.
  121. If you search for Peter Norvig's library in Google Books, I have some there.
  122. With this question maybe I'll go and update them some more.
  123. I can mention a few.
  124. In terms of general programming books, I really like The Practice of Programming.
  125. I like The Elements of Programming Style, which is rather an old
  126. and somewhat dated book now in terms of the examples it uses,
  127. but still really up-to-date in terms of the advice.
  128. I like Structure and Interpretation of Computer Programs,
  129. which has been a standard textbook for introductory computer science.
  130. I like Programming Pearls is a nice collection of short essays.
  131. Then in Python books, there's a lot of them, and I haven't kept up with all the possibilities.
  132. There's a nice online book of How To Think Like a Computer Scientist that has a Python version.
  133. There's Python in a Nutshell, Python Cookbook, Learning Python.
  134. Headfirst Python, I think, is an interesting, somewhat different approach.
  135. There are a lot of them there, and I'll try to update my Google Books library
  136. to give a little bit of commentary on each one.
  137. Thank you. Thank you for all the question. Thank you for your answer.
  138. Stay tuned for the next unit, which will be posted on Sunday
  139. and also for a couple more editions to the Python glossary.
  140. All right. See you in class.