On Sun, May 17, 2020 at 10:12 PM Ethan Furman <et...@stoneleaf.us> wrote:
> On 05/17/2020 10:18 AM, Alex Hall wrote: > > > But it's good that we have the assert statement, because it makes it > easy to write safe code and so people are encouraged to do so. > > I could not disagree more strongly with this. Every time I have seen > assert used it was in such a way that the program could easily silently > produce erroneous results or fail far from the error if asserts were turned > off. > I assume you mean that you'd like the condition in the assert to always be checked (-O or not), not that the asserts actually change behaviour. But then, isn't that how all asserts are? The only way turning off an assert can't be a problem is if you're sure the condition is always true, and then it's not needed at all. Anyway, I don't think anyone is arguing that strict zip should be turned off by -O, so for a closer analogy, let's similarly imagine that asserts can't be turned off, and they're just a convenient way to check correctness. In that case assert would probably just be a function `assert(condition, message)` since there wouldn't be a need for a special syntax. We'd be faced with the same choice - builtin or standard library import? Again, I think it'd be best as a builtin to make checking for correctness as frictionless as possible. Actually in that situation many would argue not to include such a feature in the language at all, saying it's easy enough to use an if statement or define your own function. But that would again discourage people when they're feeling lazy and they'd just leave out the check entirely. Back to the real world. Consider the problem I think you're talking about: someone has used assert when you think they should have used if+raise to make sure the check is always there. Sometimes this is because they don't know asserts might be turned off, but there are other times when they know and just don't care enough. I know that's been me sometimes. That's evidence that programmers are lazy and will often choose the *slightly* more convenient option over safety. Also note that no one (AFAIK) solves this problem by writing their own function assert_(condition, message). It would be trivial, but writing it and importing it doesn't feel like it's worth the effort.
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/2QUTXTPNUMUCMGDNQ6ILR2BT72PJ4U7T/ Code of Conduct: http://python.org/psf/codeofconduct/