Guest User

Untitled

a guest
Jul 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. Oh, there is in fact some magic behind that. Python treats "assert" as a special case; in Optimize mode (-O or -OO), the Python compiler completely removes the assert and its expression from the resulting bytecode.
  2.  
  3. So, we sometimes use this magic to optimize statements that we only want in the development environment, but not in the production environment. Usually, it would only be done on a "debug" type notify, which will be turned off in production anyway. So:
  4.  
  5. self.notify.debug("Here's some debug output")
  6.  
  7. and:
  8.  
  9. assert self.notify.debug("Here's some debug output")
  10.  
  11. Would both produce exactly the same result in a normal production environment (no output at all), but the former will result in a runtime test of the current state of self.notify.__debug, while the latter will result in no code at all.
  12.  
  13. As far as I know, assert is the *only* Python function with special compile-out magic built in like this.
Add Comment
Please, Sign In to add comment