English subtitles

← 03-34 Update Wrapper

dummy description

Get Embed Code
1 Language

Showing Revision 1 created 04/29/2012 by Amara Bot.

  1. Now how do we use this?
  2. Well, we take a function we define, say seq of x, y,
  3. and then we can say sequence is redefined as being an n_ary function of sequence.
  4. Oops--I guess I got to fix this typo here.
  5. From now on, I can call sequence and pass in any number of numbers,
  6. and it will return the result that looks like that.
  7. So that looks good.
  8. In fact, this pattern is so common in Python that there's a special notation for it.
  9. The notation is called the decorator notation.
  10. It looks like this.
  11. All we have to do is say, @ sign, then the name of a function, and then the definition.
  12. This is the same as saying sequence = n_ary of sequence.
  13. It's just an easier way to write it.
  14. But there is one problem with the way we specified this particular decorator,
  15. which is if I'm in an interactive session, and I ask for help on sequence,
  16. I would like to see the argument list and if there is a doc string,
  17. I want to see the documentation here.
  18. I didn't happen to put in any documentation for sequence.
  19. But when I ask for help, what I get is this.
  20. I'm told that sequence is called n_ary function. Well, why is that?
  21. Because this is what we returned when we define sequence = n_ary of sequence.
  22. We return this thing that has the name n_ary function.
  23. So we would like to fix n_ary up so that when the object that it returns
  24. has the same function name and the same function documentation--
  25. if there is any documentation--and have that copied over into the n_ary f function.
  26. Now it turns out that there is a function to do exacty that,
  27. and so I'm going to go get it.
  28. I'm going to say from the functools--the functional tools package.
  29. I want to import the function called update_wrapper.
  30. Update_wrapper takes 2 functions, and it copies over the function name
  31. and the documentation and several other stuff
  32. from the old function to the new function,
  33. and I can change n_ary to do that, so once I've defined the n_ary function,
  34. then I can go ahead and update the wrapper of the n_ary function--
  35. the thing I'm going to be returning from the old function.
  36. So this will be the old sequence, which has a sequence name, a list of arguments,
  37. maybe some documentation string, and this will be the function that we were returning,
  38. and we're copying over everything from f into n_ary f.
  39. Now when I ask for help--when I define n_ary sequence, and I ask for help on sequence,
  40. what I'll see is the correct name for sequence,
  41. and if there was any documentation string for sequence, that would appear here as well.
  42. So update_wrappers is a helpful tool. It helps us when we're debugging.
  43. It doesn't really help us in the execution of the program, but in doing debugging,
  44. it's really helpful to know what the correct names of your functions are.
  45. Notice that we may be violating the Don't Repeat Yourself principle here.
  46. So this n_ary function is a decorator that I'm using in this form to update
  47. the definition of sequence.
  48. I had to--within my definition of n_ary--I had to write down that I want to update the wrapper.
  49. But it seems like I'm going to want to update the wrapper for every decorator,
  50. not just for n_ary, and I don't want to repeat myself on every decorator
  51. that I'm going to define.