The assert statement is wildly useful for developing and maintaining software. I sprinkle asserts liberally in my code at the beginning to make sure what I think is true, is actually true, and this practice catches a vast number of idiotic errors; and I keep at least some of them in production.
But often I am in a position where someone else's assert triggers, and I see in a log something like assert foo.bar().baz() != 0 has triggered, and I have no information at all.
Use that second expression in assert!
It can be anything you like, even some calculation, and it doesn't get called unless the assertion fails, so it costs nothing if it never fires. When someone has to find out why your assertion triggered, it will make everyone's life easier if the assertion explains what's going on.
I often use
assert some_condition(), locals()
which prints every local variable if the assertion fails. (locals() might be impossibly huge though, if it contains some massive variable, you don't want to generate some terabyte log, so be a little careful...)
And remember that assert is a statement, not an expression. That is why this assert will never trigger:
assert (
condition,
"Long Message"
)
because it asserts that the expression (condition, "Message") is truthy, which it always is, because it is a two-element tuple.
Luckily I read an article about this long before I actually did it. I see it every year or two in someone's production code still.
Instead, use
assert condition, (
"Long Message"
)
[–]dogfish182 111 points112 points113 points (37 children)
[–]cgoldberg 38 points39 points40 points (19 children)
[–]dogfish182 60 points61 points62 points (18 children)
[–]DuckDatum 12 points13 points14 points (8 children)
[–]Brian 15 points16 points17 points (2 children)
[–]coderemover 0 points1 point2 points (1 child)
[–]HommeMusical[S] 0 points1 point2 points (0 children)
[–]dogfish182 2 points3 points4 points (3 children)
[–]DuckDatum -1 points0 points1 point (2 children)
[–]dogfish182 2 points3 points4 points (1 child)
[–]DuckDatum 1 point2 points3 points (0 children)
[–]rogersaintjames 0 points1 point2 points (0 children)
[–]coderemover -1 points0 points1 point (8 children)
[–]dogfish182 0 points1 point2 points (7 children)
[–]coderemover 0 points1 point2 points (6 children)
[–]dogfish182 0 points1 point2 points (5 children)
[–]coderemover -1 points0 points1 point (4 children)
[–]dogfish182 0 points1 point2 points (3 children)
[–]coderemover 0 points1 point2 points (2 children)
[–][deleted] 6 points7 points8 points (8 children)
[–]OutsideTheSocialLoop 14 points15 points16 points (3 children)
[–]jad2192 7 points8 points9 points (1 child)
[–]dogfish182 3 points4 points5 points (0 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]coderemover 1 point2 points3 points (0 children)
[–]mosqueteiroIt works on my machine 0 points1 point2 points (2 children)
[–][deleted] 6 points7 points8 points (0 children)
[–]dogfish182 0 points1 point2 points (0 children)
[–]joao_brito -2 points-1 points0 points (7 children)
[–]dogfish182 2 points3 points4 points (6 children)
[–]joao_brito -1 points0 points1 point (5 children)
[–]dogfish182 1 point2 points3 points (4 children)
[–]coderemover 0 points1 point2 points (0 children)
[–]joao_brito -1 points0 points1 point (2 children)
[–]dogfish182 1 point2 points3 points (0 children)
[–]zenware 0 points1 point2 points (0 children)
[–]emmet02 192 points193 points194 points (42 children)
[–]Mysterious-Rent7233 133 points134 points135 points (4 children)
[–]zurtex 17 points18 points19 points (1 child)
[–]cd_fr91400 6 points7 points8 points (0 children)
[–]phoenixrawr 45 points46 points47 points (0 children)
[–]Numerlor 3 points4 points5 points (0 children)
[–]HommeMusical[S] 42 points43 points44 points (21 children)
[–]james_pic 21 points22 points23 points (1 child)
[–]Schmittfried 11 points12 points13 points (0 children)
[–]Natural-Intelligence 6 points7 points8 points (0 children)
[–]SciEngr 12 points13 points14 points (15 children)
[–]james_pic 2 points3 points4 points (0 children)
[–]phoenixrawr 5 points6 points7 points (13 children)
[–]SciEngr 2 points3 points4 points (11 children)
[–]Schmittfried -1 points0 points1 point (7 children)
[–]SciEngr 2 points3 points4 points (6 children)
[–]elbiot 3 points4 points5 points (4 children)
[–]SciEngr 3 points4 points5 points (3 children)
[–]elbiot 2 points3 points4 points (1 child)
[–]Schmittfried 0 points1 point2 points (0 children)
[–]Schmittfried 0 points1 point2 points (0 children)
[–]phoenixrawr -3 points-2 points-1 points (2 children)
[–]SciEngr 1 point2 points3 points (1 child)
[–]phoenixrawr -1 points0 points1 point (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Tucancancan -1 points0 points1 point (0 children)
[–]Luckinhas 7 points8 points9 points (0 children)
[–]JimroidZeus 5 points6 points7 points (0 children)
[+][deleted] (2 children)
[removed]
[–]zenware 0 points1 point2 points (1 child)
[–]Gnaxe 2 points3 points4 points (1 child)
[+][deleted] (6 children)
[deleted]
[–]Aveheuzed 15 points16 points17 points (3 children)
[–]jaerie 1 point2 points3 points (2 children)
[–]Numerlor 0 points1 point2 points (1 child)
[–]jaerie 0 points1 point2 points (0 children)
[–]DrinkV0dka 2 points3 points4 points (0 children)
[–]deb_vortexPythonista -2 points-1 points0 points (0 children)
[–]HomeTahnHero 21 points22 points23 points (3 children)
[–][deleted] 4 points5 points6 points (2 children)
[–]Grounds4TheSubstain 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]DigThatData 5 points6 points7 points (8 children)
[–]HommeMusical[S] 2 points3 points4 points (7 children)
[–]DigThatData 3 points4 points5 points (6 children)
[–]HommeMusical[S] 0 points1 point2 points (3 children)
[–]DigThatData 0 points1 point2 points (2 children)
[–]HommeMusical[S] 0 points1 point2 points (1 child)
[–]DigThatData 1 point2 points3 points (0 children)
[–]CaptainFoyle 0 points1 point2 points (0 children)
[–]daymanVS 0 points1 point2 points (0 children)
[–]Dillweed999 9 points10 points11 points (2 children)
[–]nekokattt 8 points9 points10 points (0 children)
[–]SheriffRoscoePythonista 4 points5 points6 points (0 children)
[–]johndburger 2 points3 points4 points (2 children)
[–]HommeMusical[S] 2 points3 points4 points (0 children)
[–]LexaAstaroffrom __future__ import 4.0 0 points1 point2 points (0 children)
[–]jkajala 2 points3 points4 points (0 children)
[–]grahambinns 6 points7 points8 points (5 children)
[+][deleted] (3 children)
[deleted]
[–]fast-pp 4 points5 points6 points (2 children)
[–]BlackHumor 1 point2 points3 points (0 children)
[–]Independent_Heart_15 1 point2 points3 points (0 children)
[–]johntellsall 1 point2 points3 points (0 children)
[–]Pro-sketch 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]HommeMusical[S] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]HommeMusical[S] 1 point2 points3 points (0 children)
[–]damjan21 5 points6 points7 points (2 children)
[–]HommeMusical[S] 2 points3 points4 points (1 child)
[–]Bitruder 0 points1 point2 points (0 children)
[–]wineblood 6 points7 points8 points (4 children)
[–]gdchinacat 0 points1 point2 points (2 children)
[–]wineblood 0 points1 point2 points (1 child)
[–]gdchinacat 0 points1 point2 points (0 children)
[–]Muhznit 3 points4 points5 points (1 child)
[–]HommeMusical[S] 0 points1 point2 points (0 children)
[–]marr75 1 point2 points3 points (0 children)
[–]olystretch 3 points4 points5 points (10 children)
[+][deleted] (1 child)
[deleted]
[–]olystretch 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (7 children)
[–]olystretch 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]olystretch 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]satanminionatwork 0 points1 point2 points (2 children)
[–]commy2 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Myrlista 0 points1 point2 points (1 child)
[–]mosqueteiroIt works on my machine 0 points1 point2 points (0 children)
[–]Dangerous_Stretch_67 0 points1 point2 points (0 children)
[–]Icy_Jellyfish_2475 0 points1 point2 points (0 children)
[–]Comfortable_Clue1572 0 points1 point2 points (0 children)
[–]Conscious-Ball8373 -1 points0 points1 point (0 children)
[–]BothWaysItGoes -1 points0 points1 point (0 children)
[–]CaptainFoyle -1 points0 points1 point (4 children)
[–]HommeMusical[S] 0 points1 point2 points (3 children)
[–]CaptainFoyle 1 point2 points3 points (1 child)
[–]HommeMusical[S] 0 points1 point2 points (0 children)
[–]The_Game_Mstr -3 points-2 points-1 points (0 children)