hi there serdar yagerlab for infoworld here in this episode of smartpython we'll be looking at
python's assert statement and how this handy tool for debugging and testing code can be
easily misused in python a cert is a keyword that's used in conjunction with an expression
if the expression evaluates to true then the program continues normally if the expression
evaluates to false then the program throws a specialized exception assertion error we can also
provide an error message to go with the exception and we can also disable assertions at a high level
by way of a command line flag passed to the python interpreter or an environment variable
this is when we want to run code in production mode versus when we're in development mode
in this example for using an assertion i have a class named movie which takes in a year as
its sole argument when we first create the class instance we use an assert to ensure that we have
an integer and that this integer is larger than the date of the oldest movie known to history
so if we run this program the first instance we create of my movie will work
but the second instance will throw an exception because we didn't supply a year
now the key thing about assertions is that they are a tool for checking your code while you're
writing testing and debugging it in the early phases during the development process
they are prelude to eventually replacing with more robust error checking code asserts are
meant to guard against conditions that should in theory never arise and would be fatal if they did
that indicates your program has a bug that would mean handling you don't want to use assertions
as substitutes for actual runtime checks in fact that's the mistake that i've made here
let me explain why this is the case i mentioned earlier that assertions can be disabled when you
run a python program the default behavior is to enable debugging options including assertions
but when you run code in production with flags to indicate this pass to the command line
assertions are disabled this you typically do by passing the dash o or dash oo flags
at the command line to the python interpreter now here's what happens with this program when
we do that if we run with assertions disabled by way of passing this flag
we get no crash message worse we get a nonsensical date for a year so in production we need to have
code that will raise an exception which we can catch and something separate from assertion error
or which will more elegantly handle the date issue in this case my inclination is to throw an
exception since that indicates an explicit problem it's always better to make noise about a problem
than to smother it at the risk of delivering a nonsense value that could get used badly later on
so here we have a modified version of our program that raises an actual proper exception if the date
value for the movie is wrong if we run this in debug mode it throws errors as it should
and if we run it in production mode it also throws errors as it should now generally
you want to use assertions in two places one as we've seen here is when you're still developing a
program when you're still getting all the pieces to fit together and you want to ensure certain
fatal conditions never arise within the context of what you're developing basically as sanity checks
the other and more permanent place for assertions as part of a program's test suite
and here is a quick and dirty example i've put together a very simple test suite for this program
using the unit test system that's built into python this uses assert explicitly
in this first example which is one that we know is a passing test it also uses assert
indirectly as part of unit tests helper functions which employ assert as part of the way they work
so if we run this we're going to get a 100 passing score because one of the tests that
we wrote attempts to generate a failure condition and our test anticipates that
so to sum up assertions are best used in your code early in the development process as a way
to guard against conditions that shouldn't arise and would create fatal problems if they did
later when you move code into production the assertions should be migrated into test suites and
in the body of your code assertions should not be used they should be replaced with actual runtime
check code to determine if conditions are correct you should never use assertions as substitutes for
runtime checks in production the best place for them in the longest run is in your test suite
so that's it for this video if you liked it please leave a comment below don't forget
to subscribe to the tech talk channel on youtube and for more smart python
and more dev with serdar be sure to follow me on facebook youtube and infoworld.com
you