{ "metadata": { "name": "oopII" }, "nbformat": 2, "worksheets": [ { "cells": [ { "cell_type": "markdown", "source": [ "### Classes have a bunch of special methods ###", "the mirror of `__init__` is `__del__`", "(it is the tear down during clean up)" ] }, { "cell_type": "code", "input": [ "class Bear:", " def __init__(self,name):", " self.name = name", " print \" made a bear called %s\" % (name)", " def __del__(self):", " print \"Bang! %s is no longer.\" % self.name" ], "language": "python", "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "input": [ "y = Bear(\"Yogi\") ; c = Bear(\"Cal\")" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " made a bear called Yogi", " made a bear called Cal", "" ] } ], "prompt_number": 2 }, { "cell_type": "code", "input": [ "del y; del c" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Bang! Yogi is no longer.", "Bang! Cal is no longer.", "" ] } ], "prompt_number": 3 }, { "cell_type": "code", "input": [ "## note that I'm assigning y twice here", "y = Bear(\"Yogi\") ; y = Bear(\"Cal\") " ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " made a bear called Yogi", " made a bear called Cal", "Bang! Yogi is no longer.", "" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "source": [ "If we quit (not from the notebook but the interpreter or ipython)", "", " >>> f = Bear(\"Fuzzy\")", " ` made a bear called Fuzzy`", " >>> exit()", " `Bang! Fuzzy is no longer.`", " BootCamp>", "" ] }, { "cell_type": "markdown", "source": [ "note: neither `__init__` or `__del__` are allowed to return anything" ] }, { "cell_type": "code", "input": [ "run bear" ], "language": "python", "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "input": [ "a = Bear(\"Yogi\")" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " made a bear called Yogi", "" ] } ], "prompt_number": 6 }, { "cell_type": "code", "input": [ "b = Bear(\"Fuzzy\")" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " made a bear called Fuzzy", "" ] } ], "prompt_number": 7 }, { "cell_type": "code", "input": [ "Bear.bear_num " ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 8, "text": [ "2" ] } ], "prompt_number": 8 }, { "cell_type": "code", "input": [ "del a; del b" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Bang! Yogi is no longer.", "Bang! Fuzzy is no longer.", "" ] } ], "prompt_number": 9 }, { "cell_type": "code", "input": [ "Bear.bear_num" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 10, "text": [ "0" ] } ], "prompt_number": 10 }, { "cell_type": "code", "input": [ "!cat bear.log" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[2012-08-20 22:01:59.391196] created bear #1 named Yogi", "[2012-08-20 22:01:59.838567] created bear #2 named Fuzzy", "[2012-08-20 22:02:00.931343] deleted bear #1 named Yogi", "[2012-08-20 22:02:00.931508] deleted bear #2 named Fuzzy", "[2012-08-20 22:02:17.278862] created bear #1 named Fuzzy", "[2012-08-20 22:02:18.477903] created bear #2 named Yogi", "[2012-08-20 22:02:48.398837] created bear #1 named Yogi", "[2012-08-20 22:02:48.398913] deleted bear #2 named Yogi", "[2012-08-20 22:03:03.294187] created bear #2 named Yogi", "[2012-08-20 22:03:03.294255] deleted bear #1 named Yogi", "[2012-08-20 22:03:45.847077] created bear #1 named Yogi", "[2012-08-20 22:03:45.862344] created bear #2 named Fuzzy", "[2012-08-20 22:03:45.896815] deleted bear #1 named Yogi", "[2012-08-20 22:03:45.896992] deleted bear #2 named Fuzzy", "" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "source": [ "### Classes have a bunch of special methods ###", "", "`__str__` is a method that defines how a Class should represent itself as a string", "", "it takes only self as an arg, must return a string" ] }, { "cell_type": "code", "input": [ "run bear" ], "language": "python", "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "input": [ "b = Bear(\"Fuzzy\")" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " made a bear called Fuzzy", "" ] } ], "prompt_number": 13 }, { "cell_type": "code", "input": [ "print b" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " name = Fuzzy bear number = 1 (population 1)", "" ] } ], "prompt_number": 14 }, { "cell_type": "code", "input": [ "a = Bear(\"Yogi\")" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " made a bear called Yogi", "" ] } ], "prompt_number": 15 }, { "cell_type": "code", "input": [ "print b" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " name = Fuzzy bear number = 1 (population 2)", "" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "source": [ "this is the kind of formatting that datetime() is doing in it's own `__str__`" ] }, { "cell_type": "code", "input": [ "# add some dynamic aging to the bears", "from bear2 import Bear as Bear2" ], "language": "python", "outputs": [], "prompt_number": 17 }, { "cell_type": "code", "input": [ "a = Bear2(\"Yogi\")" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " made a bear called Yogi", "Bang! Yogi is no longer.", "" ] } ], "prompt_number": 18 }, { "cell_type": "code", "input": [ "print a" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " name = Yogi bear (age 0:00:17.888948) number = 1 (population 1)", "" ] } ], "prompt_number": 20 }, { "cell_type": "code", "input": [ "print a" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " name = Yogi bear (age 0:00:21.224760) number = 1 (population 1)", "" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "source": [ "### Emulating Numeric operations ###", "you can define a whole bunch of ways that instances behave upon numerical operation", "", "(e.g., `__add__` is what gets called when you type instance_1 + instance_2)" ] }, { "cell_type": "code", "input": [ "from bear1 import Bear as Bear1" ], "language": "python", "outputs": [], "prompt_number": 22 }, { "cell_type": "code", "input": [ "y = Bear1(\"Yogi\") ; c = Bear1(\"Fuzzy\")" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " made a bear called Yogi", "Bang! Cal is no longer.", " made a bear called Fuzzy", "" ] } ], "prompt_number": 23 }, { "cell_type": "code", "input": [ "our_kid = y + c" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " made a bear called progeny_of_Yogi_and_Fuzzy", "" ] } ], "prompt_number": 24 }, { "cell_type": "code", "input": [ "our_kid.parents" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 25, "text": [ "(, )" ] } ], "prompt_number": 25 }, { "cell_type": "code", "input": [ "our_kid.parents[0].name" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 26, "text": [ "'Yogi'" ] } ], "prompt_number": 26 }, { "cell_type": "code", "input": [ "our_kid.parents[1].name" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 27, "text": [ "'Fuzzy'" ] } ], "prompt_number": 27 }, { "cell_type": "code", "input": [ "our_kid1 = y * c" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " made a bear called progeny_of_Yogi_and_Fuzzy", "" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "source": [ "### Other Useful Specials ###" ] }, { "cell_type": "code", "input": [ "print Bear1.__doc__", "print Bear1.__name__", "print Bear1.__module__", "print Bear1.__bases__", "print Bear1.__dict__" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "", " class to show off addition (and multiplication)", " ", "Bear", "bear1", "()", "{'__module__': 'bear1', 'bear_num': 4, '__mul__': , '__add__': , '__doc__': '\\n class to show off addition (and multiplication)\\n ', '__init__': }", "" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "source": [ "### A note on referencing... ###" ] }, { "cell_type": "code", "input": [ "a = Bear(\"Yogi\")" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " made a bear called Yogi", "" ] } ], "prompt_number": 31 }, { "cell_type": "code", "input": [ "a" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 32, "text": [ "<__main__.Bear instance at 0x1b5d6c0>" ] } ], "prompt_number": 32 }, { "cell_type": "code", "input": [ "b = a" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Bang! Fuzzy is no longer.", "" ] } ], "prompt_number": 33 }, { "cell_type": "code", "input": [ "b" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 34, "text": [ "<__main__.Bear instance at 0x1b5d6c0>" ] } ], "prompt_number": 34 }, { "cell_type": "code", "input": [ "a.name = \"Fuzzy\"" ], "language": "python", "outputs": [], "prompt_number": 35 }, { "cell_type": "code", "input": [ "b.name" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 36, "text": [ "'Fuzzy'" ] } ], "prompt_number": 36 }, { "cell_type": "code", "input": [ "Bear.bear_num" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 37, "text": [ "1" ] } ], "prompt_number": 37 }, { "cell_type": "code", "input": [ "import copy" ], "language": "python", "outputs": [], "prompt_number": 38 }, { "cell_type": "code", "input": [ "c = copy.copy(a)" ], "language": "python", "outputs": [], "prompt_number": 39 }, { "cell_type": "code", "input": [ "c # new memory location" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 40, "text": [ "<__main__.Bear instance at 0x1b7fdf0>" ] } ], "prompt_number": 40 }, { "cell_type": "code", "input": [ "Bear.bear_num" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 41, "text": [ "1" ] } ], "prompt_number": 41 }, { "cell_type": "code", "input": [ "c.name" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 42, "text": [ "'Fuzzy'" ] } ], "prompt_number": 42 }, { "cell_type": "code", "input": [ "c.name = \"Smelly\"" ], "language": "python", "outputs": [], "prompt_number": 43 }, { "cell_type": "code", "input": [ "a.name" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 44, "text": [ "'Fuzzy'" ] } ], "prompt_number": 44 }, { "cell_type": "code", "input": [ "a.mylist = [1,2,3]" ], "language": "python", "outputs": [], "prompt_number": 45 }, { "cell_type": "code", "input": [ "c.mylist" ], "language": "python", "outputs": [ { "ename": "AttributeError", "evalue": "Bear instance has no attribute 'mylist'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmylist\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: Bear instance has no attribute 'mylist'" ] } ], "prompt_number": 46 }, { "cell_type": "code", "input": [ "d = copy.copy(a)", "d.mylist" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 47, "text": [ "[1, 2, 3]" ] } ], "prompt_number": 47 }, { "cell_type": "code", "input": [ "d.name" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 48, "text": [ "'Fuzzy'" ] } ], "prompt_number": 48 }, { "cell_type": "code", "input": [ "d.name = \"Yogi\"" ], "language": "python", "outputs": [], "prompt_number": 49 }, { "cell_type": "code", "input": [ "a.name" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 50, "text": [ "'Fuzzy'" ] } ], "prompt_number": 50 }, { "cell_type": "code", "input": [ "a.mylist[0] = -1" ], "language": "python", "outputs": [], "prompt_number": 52 }, { "cell_type": "code", "input": [ "d.mylist" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 54, "text": [ "[-1, 2, 3]" ] } ], "prompt_number": 54 }, { "cell_type": "code", "input": [ "e = copy.deepcopy(a)" ], "language": "python", "outputs": [], "prompt_number": 55 }, { "cell_type": "code", "input": [ "a.mylist[0] = \"a\"" ], "language": "python", "outputs": [], "prompt_number": 56 }, { "cell_type": "code", "input": [ "e.mylist" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 57, "text": [ "[-1, 2, 3]" ] } ], "prompt_number": 57 }, { "cell_type": "markdown", "source": [ "`deepcopy`: copies all attributes pointed to internally" ] }, { "cell_type": "code", "input": [ "" ], "language": "python", "outputs": [] } ] } ] }