{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "(assert)=\n", "# Assert \n", "\n", "Assert statement allows us to do sanity checks in our code. It is also a very handy tool in preventing bugs that do not throw an `Error`. Consider the following factorial function:\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def fact(n):\n", " result = 1\n", " for i in range(n):\n", " result*=i\n", " return result\n", "fact(-1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if \\\\(n = 1.5\\\\), \\\\(n = -1\\\\) or \\\\(n \\\\) is not a numeric type? Our function is defined for non-negative integers only. However, for some of those invalid inputs, the function can actually return something! `assert` is a great feature to add here:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "ename": "AssertionError", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m*=\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mfact\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m\u001b[0m in \u001b[0;36mfact\u001b[1;34m(n)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mfact\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[0mn\u001b[0m \u001b[1;32mis\u001b[0m \u001b[0mint\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mn\u001b[0m \u001b[1;33m>=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m*=\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mAssertionError\u001b[0m: " ] } ], "source": [ "def fact(n):\n", " assert n is int and n >=0\n", " result = 1\n", " for i in range(n):\n", " result*=i\n", " return result\n", "fact(-1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now our program throws an `AssertionError` and we immediately know that the input is invalid. While in this simple example such precautions might seem to be an overkill, `assert` scales very well. When multiple functions exchange data, this feature might shorten the debugging time **a lot**.\n", "If you do not want your program to crash because of the `AssertionError` surround your code with the `try..except` block: " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is an invalid input\n" ] } ], "source": [ "def fact(n):\n", " try:\n", " assert n is int and n >=0\n", " result = 1\n", " for i in range(n):\n", " result*=i\n", " return result\n", " except AssertionError:\n", " print(\"This is an invalid input\")\n", "\n", "fact(-1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In some coding regimes, `assert` is considered acceptable only in the development process. It is often `deactivated` in the final version of the software." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(assert_exercises)=\n", "## Exercises\n", "\n", "* **Perimeter of a circle** Define a function which calculates the perimeter of a circle given its radius.\n", "\n", "**HINT**: `import math` for the value of \\\\(\\pi\\\\). Do not assume that `r` is of any particular type." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "````{admonition} Answer\n", ":class: dropdown\n", "\n", "```python\n", "from math import pi\n", "\n", "def perimeter(r):\n", " assert r >=0\n", " return 2* math.pi*r\n", " \n", "```\n", "\n", "````" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }