Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "metadata": {
- "name": "ch 8 classes and objects"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Things:\n pass",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 101
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Inanimate(Things):\n pass",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 102
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Animate(Things):\n pass",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 103
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Sidewalks(Inanimate):\n pass",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 104
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Animals(Animate):\n pass",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 105
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Mammals(Animals):\n pass",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 106
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Giraffes(Mammals):\n pass",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 107
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "reginald = Giraffes()",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 108
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "print(Giraffes())# printing the class doesn't do anything...yet?!",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "<__main__.Giraffes object at 0x3ef4310>\n"
- }
- ],
- "prompt_number": 109
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 109
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Animals(Animate):\n def breathe(self):\n pass\n def move(self):\n pass\n def eat_food(self):\n pass",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 110
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Mammals(Animals):\n def feed_young_with_milk(self):\n pass\nclass Giraffes(Mammals):\n def eat_leaves_from_trees(self):\n pass",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 111
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "reginald= Giraffes()\nreginald.move()\nreginald.eat_leaves_from_trees()",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 112
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "harold = Giraffes()",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 113
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "harold.move()",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 114
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Animals(Animate):\n def breathe(self):\n print('breathing')\n def move(self):\n print('moving')\n def eat_food(self):\n print('eating food')",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 115
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Mammals(Animals):\n def feed_young_with_milk(self):\n print('feeding young')\nclass Giraffes(Mammals):\n def eat_leaves_from_trees(self):\n print('eating leaves')\n def find_food(self):\n self.move()\n print (\"I've found food\")\n self.eat_food()\n def dance_a_jig(self):\n for i in range (10):\n self.move()\n print('dance')\n def take_a_nap(self):\n for i in range(5):\n self.move()\n print(\"I'm so sleepy...\")",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 157
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "reginald = Giraffes()\nharold = Giraffes()\nharold.\nreginald.eat_leaves_from_trees()\nreginald.breathe()\nharold.feed_young_with_milk()\nharold.dance_a_jig()",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "ename": "SyntaxError",
- "evalue": "invalid syntax (<ipython-input-156-02308f850bda>, line 3)",
- "output_type": "pyerr",
- "traceback": [
- "\u001b[1;36m File \u001b[1;32m\"<ipython-input-156-02308f850bda>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m harold.\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
- ]
- }
- ],
- "prompt_number": 156
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "reginald.breathe()",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "breathing\n"
- }
- ],
- "prompt_number": 118
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "harold.move()",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "moving\n"
- }
- ],
- "prompt_number": 119
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "reginald.eat_food()",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "eating food\n"
- }
- ],
- "prompt_number": 120
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "reginald.feed_young_with_milk()",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "feeding young\n"
- }
- ],
- "prompt_number": 121
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "class Giraffes(Mammals):\n def find_food(self):\n self.move()\n print (\"I've found food\")\n self.eat_food()",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 122
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "harold.breathe()\nreginald.breathe()\nharold.",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "breathing\nbreathing\n"
- }
- ],
- "prompt_number": 123
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "reginald.feed_young_with_milk()",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "feeding young\n"
- }
- ],
- "prompt_number": 124
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "def eat_leaves_from_trees(self):\n self.eat_food()",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 131
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "reginald.eat_food()\nreginald.find_food()\nharold.eat_leaves_from_trees()\n",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "'Giraffes' object has no attribute 'find_food'",
- "output_type": "pyerr",
- "traceback": [
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
- "\u001b[1;32m<ipython-input-132-873f96003fcf>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mreginald\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0meat_food\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mreginald\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_food\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mharold\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0meat_leaves_from_trees\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;31mAttributeError\u001b[0m: 'Giraffes' object has no attribute 'find_food'"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "eating food\n"
- }
- ],
- "prompt_number": 132
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "harold.eat_lea",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "'Giraffes' object has no attribute 'eat_lea'",
- "output_type": "pyerr",
- "traceback": [
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
- "\u001b[1;32m<ipython-input-127-c652efb4d875>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mharold\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0meat_lea\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[1;31mAttributeError\u001b[0m: 'Giraffes' object has no attribute 'eat_lea'"
- ]
- }
- ],
- "prompt_number": 127
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "reginald.breathe()",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "breathing\n"
- }
- ],
- "prompt_number": 128
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "def dance_a_jig(self):\n self.move()\n print('dance')\n",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 139
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "dance_a_jig(1)",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "'int' object has no attribute 'move'",
- "output_type": "pyerr",
- "traceback": [
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
- "\u001b[1;32m<ipython-input-141-3e182466538a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdance_a_jig\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[1;32m<ipython-input-139-13daad1cb54c>\u001b[0m in \u001b[0;36mdance_a_jig\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mdance_a_jig\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'dance'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;31mAttributeError\u001b[0m: 'int' object has no attribute 'move'"
- ]
- }
- ],
- "prompt_number": 141
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "harold.dance_a_jig()\nreginald.eat_food()\n",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "'Giraffes' object has no attribute 'dance_a_jig'",
- "output_type": "pyerr",
- "traceback": [
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
- "\u001b[1;32m<ipython-input-138-e8623aa3255b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mharold\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdance_a_jig\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mreginald\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0meat_food\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;31mAttributeError\u001b[0m: 'Giraffes' object has no attribute 'dance_a_jig'"
- ]
- }
- ],
- "prompt_number": 138
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "harold.",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 130
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "??Giraffes",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 142
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "?Giraffes",
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 143
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "harold.feed_young_with_milk()",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "feeding young\n"
- }
- ],
- "prompt_number": 152
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "",
- "language": "python",
- "metadata": {},
- "outputs": []
- }
- ],
- "metadata": {}
- }
- ]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement