{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "class NegatableString(str):\n", " def __rsub__(self, other):\n", " if other.endswith(self):\n", " return other[:len(other)-len(self)]\n", " else:\n", " raise ValueError(\"Cannot subtract {!r} from {!r}\".format(self, other))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "x = NegatableString(\"abc\")\n", "\"123abc\" - x" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "'123'" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "\"123def\" - x" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }