{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# %load ../example_code/item_25.py\n", "#!/usr/bin/env python3\n", "\n", "# 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": [ "## use\tthe\tsuper\tbuilt-in\tfunction\tto\tinitialize\tparent\tclasses." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "# Example 1\n", "class MyBaseClass(object):\n", " def __init__(self, value):\n", " self.value = value\n", "\n", "class MyChildClass(MyBaseClass):\n", " def __init__(self):\n", " MyBaseClass.__init__(self, 5)\n", "\n", " def times_two(self):\n", " return self.value * 2\n", "\n", "foo = MyChildClass()\n", "print(foo.times_two())" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 2\n", "class TimesTwo(object):\n", " def __init__(self):\n", " self.value *= 2\n", "\n", "class PlusFive(object):\n", " def __init__(self):\n", " self.value += 5" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 3\n", "class OneWay(MyBaseClass, TimesTwo, PlusFive):\n", " def __init__(self, value):\n", " MyBaseClass.__init__(self, value)\n", " TimesTwo.__init__(self)\n", " PlusFive.__init__(self)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First ordering is (5 * 2) + 5 = 15\n" ] } ], "source": [ "# Example 4\n", "foo = OneWay(5)\n", "print('First ordering is (5 * 2) + 5 =', foo.value)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 5\n", "class AnotherWay(MyBaseClass, PlusFive, TimesTwo):\n", " def __init__(self, value):\n", " MyBaseClass.__init__(self, value)\n", " TimesTwo.__init__(self)\n", " PlusFive.__init__(self)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Second ordering still is 15\n" ] } ], "source": [ "# Example 6\n", "bar = AnotherWay(5)\n", "print('Second ordering still is', bar.value)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 7\n", "class TimesFive(MyBaseClass):\n", " def __init__(self, value):\n", " MyBaseClass.__init__(self, value)\n", " self.value *= 5\n", "\n", "class PlusTwo(MyBaseClass):\n", " def __init__(self, value):\n", " MyBaseClass.__init__(self, value)\n", " self.value += 2" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Should be (5 * 5) + 2 = 27 but is 7\n" ] } ], "source": [ "# Example 8\n", "class ThisWay(TimesFive, PlusTwo):\n", " def __init__(self, value):\n", " TimesFive.__init__(self, value)\n", " PlusTwo.__init__(self, value)\n", "\n", "foo = ThisWay(5)\n", "print('Should be (5 * 5) + 2 = 27 but is', foo.value)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 9\n", "class MyBaseClass(object):\n", " def __init__(self, value):\n", " self.value = value\n", "\n", "class TimesFiveCorrect(MyBaseClass):\n", " def __init__(self, value):\n", " super(TimesFiveCorrect, self).__init__(value)\n", " self.value *= 5\n", "\n", "class PlusTwoCorrect(MyBaseClass):\n", " def __init__(self, value):\n", " super(PlusTwoCorrect, self).__init__(value)\n", " self.value += 2\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Should be 5 * (5 + 2) = 35 and is 35\n" ] } ], "source": [ "# Example 10\n", "class MyBaseClass(object):\n", " def __init__(self, value):\n", " self.value = value\n", "\n", "class TimesFiveCorrect(MyBaseClass):\n", " def __init__(self, value):\n", " super(TimesFiveCorrect, self).__init__(value)\n", " self.value *= 5\n", "\n", "class PlusTwoCorrect(MyBaseClass):\n", " def __init__(self, value):\n", " super(PlusTwoCorrect, self).__init__(value)\n", " self.value += 2\n", "\n", "class GoodWay(TimesFiveCorrect, PlusTwoCorrect):\n", " def __init__(self, value):\n", " super(GoodWay, self).__init__(value)\n", "\n", "foo = GoodWay(5)\n", "print('Should be 5 * (5 + 2) = 35 and is', foo.value)\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[,\n", " ,\n", " ,\n", " ,\n", " ]\n", "[,\n", " ,\n", " ,\n", " ,\n", " ]\n" ] } ], "source": [ "# Example 11\n", "# This is pretending to be Python 2 but it's not\n", "class MyBaseClass(object):\n", " def __init__(self, value):\n", " self.value = value\n", "\n", "class TimesFiveCorrect(MyBaseClass):\n", " def __init__(self, value):\n", " super(TimesFiveCorrect, self).__init__(value)\n", " self.value *= 5\n", "\n", "class PlusTwoCorrect(MyBaseClass):\n", " def __init__(self, value):\n", " super(PlusTwoCorrect, self).__init__(value)\n", " self.value += 2\n", "\n", "class GoodWay(TimesFiveCorrect, PlusTwoCorrect):\n", " def __init__(self, value):\n", " super(GoodWay, self).__init__(value)\n", "\n", "before_pprint = pprint\n", "pprint(GoodWay.mro())\n", "from pprint import pprint\n", "pprint(GoodWay.mro())\n", "pprint = pprint" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Example 12\n", "class Explicit(MyBaseClass):\n", " def __init__(self, value):\n", " super(__class__, self).__init__(value * 2)\n", "\n", "class Implicit(MyBaseClass):\n", " def __init__(self, value):\n", " super().__init__(value * 2)\n", "\n", "assert Explicit(10).value == Implicit(10).value" ] } ], "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 }