5 ms·
Cute fact about __debug__: it is one of the only ways to get compile-time conditionals in Python. Performing a comparison with `if __debug__:` will output byte
by nneonneo 5y ago
Cute fact about __debug__: it is one of the only ways to get compile-time conditionals in Python. Performing a comparison with `if __debug__:` will output byte code for the ensuing statement if and only if the interpreter is in debug mode - notably, in `-O` mode, it will not even generate a load of __debug__ and a conditional jump, and acts as if the statement didn’t exist at all.
- globular-toast 5y agoI knew that `assert` behaved in a similar way. Turns out it's actually equivalent to an `if __debug__:` https://docs.python.org/3/reference/simple_stmts.html#grammar-token-python-grammar-assert_stmt https://docs.python.org/3/reference/simple_stmts.html#gramma...
- tusharsadhwani 5y agothat is indeed interesting. Mind if I add this in the article?
- nneonneo 5y agoSure, go ahead :)
- Fordec 5y agoNot going to lie, if I could enable stricter compile time conditions without debug mode that would be a very welcome RFC
- BiteCode_dev 5y agoUnfortunatly you often can't use -o because of the 3rd party libs that didnt' get the memo and use assert for error checking. We still have -X dev and sys.flags but it's runtime only.
- alshel 5y agoCare to name any 3rd party libs in particular?
- zbentley 5y agoDjango Rest Framework.
- BiteCode_dev 5y agoNope, I just remember my server crashing years ago so I stopped ever since. Would be interesting to scan pypi and check if this is still a thing. Maybe create a bot warning lib authors.
- zarzavat 5y agoSeems like a better idea would be to make a separate flag "--remove-assertions" for people who desire that.
- BiteCode_dev 5y ago"-o" is mostly "--remove-assertions" already, so that wouldn't help. While today's use case for assert is unit testing, the actual killer feature of the keyword is that it's removed only from prod. The idea is that you can write things like function contracts, that are expensive, but only exist in dev. Now, if one of you dependencies use assert for something they expect to still be in prod, which is the problem we are talking about in the first place, "-o" or "--remove-assertions" will strip their assert too, breaking their code, and hence, yours since it depends on it.
- athrowaway3z 5y agoBefore this thread i had no idea assert was debug only in python. Another solution would be to accept the current role of assert as quick error checking and add in debug_assert to indicate a conditional error check. The biggest issue with that approach is that a majority will suddenly ask "Wait, python has a debug mode?"
- robot_no_419 5y agoUnless I'm misunderstanding, they are wrapping their asserts in try/catch blocks? That's... yikes.
- masklinn 5y agoMost likely they just `assert` error conditions and let that blow up as error reporting, rather than write a full conditional and raise a custom exception. I’ve been guilty of that in my own code when I just want to tell myself about an error.
- andi999 5y agoIsnt just assert the error condition how assert is actually intended to be used?
- masklinn 5y agoDepends. The issue is that since `assert` is stripped out in “O” mode, if the codebase depends on `assert` for correctness… they’re not compatible.
- andi999 5y agoWhat do you mean by depends on assert for correctness? How?
- Doxin 5y agoget_president_inside_bunker() assert president_is_inside_bunker() close_bunker_doors() assert bunker_doors_are_closed() send_nukes() With asserts enabled this is fine. With asserts disabled you might start sending nukes while the president is still outside or the doors to the bunker are still open.
- andi999 5y agoThanks! I never thought anybody would do such a thing.