YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← 03-44 Disable Decorator

dummy description

Get Embed Code
1 Language

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

  1. We're coming to the end of what I want to say about decorators.
  2. I wanted to add one more debug tool.
  3. That's one I'm going to call disabled.
  4. It's very simple.
  5. Disabled is another name for the identity function--
  6. that is the function that returns its argument without doing any computation on it whatsoever.
  7. Why do I want it and why do I call it "disabled?"
  8. Well, the idea is that if I'm using some of these debugging tools like trace or countcalls,
  9. I might have scattered throughout my program trace define f and some other traced functions.
  10. Then I might decide I think I'm okay now. I think I've got it debugged.
  11. I don't want to trace any more.
  12. Then what I can do is I can just say "trace = disabled"
  13. and reload my program, and now the decorator trace will be applied to the function,
  14. but what it will do is return the function itself.
  15. Notice we don't have to say that disabled is a decorator,
  16. even though we're using it as if it were one,
  17. because it doesn't create a new function. It just returns the original function.
  18. That way we won't have the trace output cluttering up our output,
  19. and the function will be efficient.
  20. There won't even be a test to see if we are tracing or not.
  21. It'll just use the exact function that passed in.