{ "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": [ "## From other language, we love to use getter and setter, but not in Python!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 1\n", "class OldResistor(object):\n", " def __init__(self, ohms):\n", " self._ohms = ohms\n", "\n", " def get_ohms(self):\n", " return self._ohms\n", "\n", " def set_ohms(self, ohms):\n", " self._ohms = ohms" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before: 50000.0\n", "After: 10000.0\n" ] } ], "source": [ "# Example 2\n", "r0 = OldResistor(50e3)\n", "print('Before: %5r' % r0.get_ohms())\n", "r0.set_ohms(10e3)\n", "print('After: %5r' % r0.get_ohms())" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 3\n", "r0.set_ohms(r0.get_ohms() + 5e3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Just use simple\tpublic\tattributes" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10000.0 ohms, 0 volts, 0 amps\n" ] } ], "source": [ "# Example 4\n", "class Resistor(object):\n", " def __init__(self, ohms):\n", " self.ohms = ohms\n", " self.voltage = 0\n", " self.current = 0\n", "\n", "r1 = Resistor(50e3)\n", "r1.ohms = 10e3\n", "print('%r ohms, %r volts, %r amps' %\n", " (r1.ohms, r1.voltage, r1.current))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 5\n", "r1.ohms += 5e3" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 6\n", "class VoltageResistance(Resistor):\n", " def __init__(self, ohms):\n", " super().__init__(ohms)\n", " self._voltage = 0\n", "\n", " @property\n", " def voltage(self):\n", " return self._voltage\n", "\n", " @voltage.setter\n", " def voltage(self, voltage):\n", " self._voltage = voltage\n", " self.current = self._voltage / self.ohms" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before: 0 amps\n", "After: 0.01 amps\n" ] } ], "source": [ "# Example 7\n", "r2 = VoltageResistance(1e3)\n", "print('Before: %5r amps' % r2.current)\n", "r2.voltage = 10\n", "print('After: %5r amps' % r2.current)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## \t@property\tdecorator\tand\tits\tcorresponding\tsetter\tattribute? Why??" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 8\n", "class BoundedResistance(Resistor):\n", " def __init__(self, ohms):\n", " super().__init__(ohms)\n", "\n", " @property\n", " def ohms(self):\n", " return self._ohms\n", "\n", " @ohms.setter\n", " def ohms(self, ohms):\n", " if ohms <= 0:\n", " raise ValueError('%f ohms must be > 0' % ohms)\n", " self._ohms = ohms" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 9\n", "try:\n", " r3 = BoundedResistance(1e3)\n", " r3.ohms = 0\n", "except:\n", " logging.exception('Expected')\n", "else:\n", " assert False" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 10\n", "try:\n", " BoundedResistance(-5)\n", "except:\n", " logging.exception('Expected')\n", "else:\n", " assert False" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 11\n", "class FixedResistance(Resistor):\n", " def __init__(self, ohms):\n", " super().__init__(ohms)\n", "\n", " @property\n", " def ohms(self):\n", " return self._ohms\n", "\n", " @ohms.setter\n", " def ohms(self, ohms):\n", " if hasattr(self, '_ohms'):\n", " raise AttributeError(\"Can't set attribute\")\n", " self._ohms = ohms" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 12\n", "try:\n", " r4 = FixedResistance(1e3)\n", " r4.ohms = 2e3\n", "except:\n", " logging.exception('Expected')\n", "else:\n", " assert False" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 13\n", "class MysteriousResistor(Resistor):\n", " @property\n", " def ohms(self):\n", " self.voltage = self._ohms * self.current\n", " return self._ohms\n", "\n", " @ohms.setter\n", " def ohms(self, ohms):\n", " self._ohms = ohms" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before: 0\n", "After: 0.1\n" ] } ], "source": [ "# Example 14\n", "r7 = MysteriousResistor(10)\n", "r7.current = 0.01\n", "print('Before: %5r' % r7.voltage)\n", "r7.ohms\n", "print('After: %5r' % r7.voltage)" ] } ], "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 }