{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n", "#\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# http://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "\n", "# Preamble to mimick book environment\n", "import logging\n", "from pprint import pprint\n", "from sys import stdout as STDOUT" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### returning None is error prone!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Example 1\n", "def divide(a, b):\n", " try:\n", " return a / b\n", " except ZeroDivisionError:\n", " return None\n", "\n", "assert divide(4, 2) == 2\n", "assert divide(0, 1) == 0\n", "assert divide(3, 6) == 0.5\n", "assert divide(1, 0) == None" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Invalid inputs\n" ] } ], "source": [ "# Example 2\n", "x, y = 1, 0\n", "result = divide(x, y)\n", "if result is None:\n", " print('Invalid inputs')\n", "else:\n", " print('Result is %.1f' % result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### This is WRONG!!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Invalid inputs\n" ] } ], "source": [ "# Example 3\n", "x, y = 0, 5\n", "result = divide(x, y)\n", "if not result:\n", " print('Invalid inputs') # This is wrong!\n", "else:\n", " assert False" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(True, 0.0)\n" ] } ], "source": [ "# Example 4\n", "def divide(a, b):\n", " try:\n", " return True, a / b\n", " except ZeroDivisionError:\n", " return False, None\n", "print(divide(0, 5))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Invalid inputs\n" ] } ], "source": [ "# Example 5\n", "x, y = 5, 0\n", "success, result = divide(x, y)\n", "if not success:\n", " print('Invalid inputs')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Invalid inputs\n", "Invalid inputs\n" ] } ], "source": [ "# Example 6\n", "x, y = 5, 0\n", "_, result = divide(x, y)\n", "if not result:\n", " print('Invalid inputs') # This is right\n", "\n", "x, y = 0, 5\n", "_, result = divide(x, y)\n", "if not result:\n", " print('Invalid inputs') # This is wrong" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Example 7\n", "def divide(a, b):\n", " try:\n", " return a / b\n", " except ZeroDivisionError as e:\n", " raise ValueError('Invalid inputs') from e" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Result is 2.5\n" ] } ], "source": [ "# Example 8\n", "x, y = 5, 2\n", "try:\n", " result = divide(x, y)\n", "except ValueError:\n", " print('Invalid inputs')\n", "else:\n", " print('Result is %.1f' % result)" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }