r/PythonLearning • u/jake_123_123 • 4d ago
Discussion why would you use assert instead of if false: raise someerror("custom message about error"). what are the benifits of assert. because it seems that it doesn't save time nor effort, nor does it communicate code better than an if with custom error message
5
Upvotes
8
u/dev-razorblade23 4d ago
assertis mostly used in tests, not in your code. If you need to act on something, useif-elseorswitch case, asserts are for tests9
u/serge-mv 4d ago
They serve different purpose. Assert is for a condition that should never be false. If assert fails, you have a bug or a catastrophic failure. if-statement is simply for different paths a code can take. Everything inside if is expected and normal. Normally you would not use asserts in production, but in tests while if-statements is a normal statement you use in code.
3
u/ConDar15 3d ago
What everyone else has said is all correct, but there is an extra thing with the
assertstatement: it is disabled when using an optimized build.This is probably well beyond the scope of what you need to be caring about right now, but when python runs it by default runs in a debug mode which has the special global
__debug__set toTrue. When you run python in optimized mode (you need to explicitly provide the-Oflag when running python) that special__debug__variable is instead set toFalse.One consequence of this is that the
assertstatement depends on__debug__and so in an optimized build does not get executed. This doesn't usually have much impact, but if some of your assertions (used to check that the program is in a valid state) are fairly expensive to compute you can leave them in place for development and testing, and then for production run an optimized build because you're confident in the system stability and can improve the speed of it.2
u/mc_pm 4d ago
Assert is for stuff that *must* be true, and where you are saying right from the code level "this indicates something is really wrong".
An exception is more about an expectation that wasn't fulfilled: the file doesn't exist, you ended up dividing by zero, the network connection couldn't be established, etc. These aren't normal problems, but they are 'expected' and you can plan for. If you put something in assert you're saying "that's it, fail out right now, the system isn't in a state I can continue with".
1
u/Living_Fig_6386 3d ago
assert is a statement that “so we are clear this must be true; otherwise something bad went wrong somewhere before this point but we don’t know where so we must stop”. It’s a handy way of aborting a program when the program state doesn’t make sense.
An exception says “this went wrong in this way” while an assert says “we got to this point and I can tell something went wrong before it, but I don’t know why”.
1
u/rghthndsd 3d ago
If you as a user of a package sees an assert raise, you immediately know you should report a bug.
1
u/billsil 3d ago
assert x> 0, ‘bad x’ tells me about the why, so I can be lazier
assert area > 0, area is even lazier. I absolutely use asserts in production. Do some type checking. Don’t ever send me bad data, but since I don’t trust myself, yeah throw a blanket catch all errors and log them.
1
u/cmdr_iannorton 1d ago
generally, its easier to write:
it is most used in writing unit tests
0
u/Usual_Office_1740 4d ago
For me it's about conveying intent. In basic general terms, an error is a problem to be handled someplace other than the site it's raised from. An assert is something that should never actually happen. If it does happen crash so I can use a debugger to know when, where, why and how.
0
u/recursion_is_love 4d ago
using assert make it easy to scan the code for quick, you don't need to read and follow every line.
0
u/Dry-Aioli-6138 4d ago
Good answers here. Also, worth reading on the topic: Stop using asset in your code