4 ms·
I honestly have never even heard of this constant and I feel like I've been using python for a pretty long time. Although maybe my memory for some things just
by plant-ian 1mo ago
I honestly have never even heard of this constant and I feel like I've been using python for a pretty long time. Although maybe my memory for some things just gets garbage collected if I don't use it enough. Does it actually get used that often in real world code? Seems like it might be kind of risky.
- Vexs 1mo agoI see asserts used in production code as part of flow control way too frequently, so I assume the majority of python users aren't aware of the -O flag, much less this behavior- which I too haven't ever heard of. Of recently, I've noticed claude is a big fan of asserts too.
- UqWBcuFx6NV4r 1mo agoYep. I’ve had to tell Claude to basically not use assert. Thankfully it’s very complaint in this one area.
- rcxdude 1mo agoI feel like it's the kind of thing you might wind up caring about if you're micro-optimizing your python, but in my experience that's a losing game and you're better served rewriting it in another language than bothering with trying to speed up the execution of the raw python code (it's not that you can't optimize python code, but only in broader strokes. If you are looking at the bytecode you're in too deep and every time I've seen it tried the code has been ported shortly afterwards).
- UqWBcuFx6NV4r 1mo agoDitto. I’ve certainly never used it and can’t recall seeing it in any codebases I’ve worked on or looked at. Sounds interesting though! I’ve of course certainly heard of, seen, and used `assert`, but more often than not, outside of pytest, I see its use way more in potential footgun scenarios—I doubt that many people know that assertions can be silenced, and that they’d probably be better off raising exceptions in many cases where they’re using `assert`.
- nneonneo 1mo agoI made a CTF problem where `assert` was used as a critical safety check - and where "accidentally" running the program under -O (for speed!) resulted in a security vulnerability. A large fraction of the people who attempted the problem seemingly missed this bug. I would not be surprised in the least if that pattern existed in the wild. In fact, it's quite common to see this in C/C++ codebases too: people will use assert() to check a security-relevant property, and then disable those checks in their release builds "because it can't happen".
- zahlman 1mo agoBecause Python lets you get as far as it does without formally learning everything, but is also expected to suit a huge variety of use cases, it ends up with lots of hidden details that are irrelevant to most users. The most direct way to find out about `__debug__` is to read `python -h` (or the usage message, which is not all that easy to trigger) in full, and then head over to the documentation. > Does it actually get used that often in real world code? https://github.com/search?q=language%3APython+%2F%28%3F-i%29__debug__%2F&type=code https://github.com/search?q=language%3APython+%2F%28%3F-i%29...