{ "metadata": { "name": "", "signature": "sha256:99b1b814b72e81eb239ade1efd783051dca7fcb4b9ff68f1c371e065d31b3038" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#Simple Bear class, attributes and methods" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Bear:\n", " print \"the bear class is now defined.\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "the bear class is now defined.\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "a = Bear # this is not generally useful: we don't often reference the class itself\n", "a" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "__main__.Bear" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "a = Bear() # that's more like it! This creates a new *instance* of the class\n", "a" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "<__main__.Bear instance at 0x8f5d54c>" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "a.name = \"Oski\" # In Python, we can add attributes to the instance on-the-fly\n", "a.color = \"Brown\"\n", "print a.name # Does he know who he is?" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Oski\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "class Bear:\n", " print \"The bear class is now defined.\"\n", " def say_hello(self): # don't worry about the 'self' just yet...\n", " print \"Hello, world! I am a bear.\"\n" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The bear class is now defined.\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "a = Bear()\n", "a.say_hello # this is just referencing the method itself. not very helpful." ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ ">" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "a.say_hello() # with ()'s it actually calls the method" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello, world! I am a bear.\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#The `__init__()` method\n", "`__init__()` is a special method automatically called when a new instance is created.
\n", "It can specify necessary initialization parameters.\n", "

\n", "\"self\" is a special identifier used inside a method to refer to the particular instance of the class. self is not explicitly passed in when accessed through an object instance; Python takes care of that bookkeeping." ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Bear:\n", " print \"Bear class ready\"\n", " def __init__(self, name):\n", " self.name = name\n", " print \"A bear is born.\"\n", " def say_hello(self):\n", " print \"Hello, world! I am a bear.\"\n", " print \"My name is %s.\" % self.name" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": true, "input": [ "a = Bear()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a = Bear(\"Yogi\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a.say_hello()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Global class variables, versus object instance attributes" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Bear:\n", " population=0\n", " def __init__(self,name):\n", " self.name=name\n", " Bear.population += 1 # Increment the 'global' census counter, a class attribute\n", " def say_hello(self):\n", " print \"Hello, world! I am a bear.\"\n", " print \"My name is %s.\" % self.name\n", " print \"I am number %i.\" % Bear.population\n", "\n", "a = Bear(\"Yogi\");\n", "a.say_hello()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "b = Bear(\"Winnie\");\n", "b.say_hello();\n", "a.say_hello()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Here you can already see interesting and useful dynamic behavior: Yogi's say_hello() method 'knows' about other bear(s)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Calling a class method \"globally\", with an explicit instance" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c = Bear(\"Fozzie\")\n", "Bear.say_hello(c) # calling the class 'directly' with an explicit reference to an object" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#A Zookeeper\u2019s Travails I\n", "Suppose you are a zookeeper. You have three\n", "bears in your care (Yogi, Winnie, and Fozzie), and\n", "you need to take them to a shiny new\n", "habitat in a different part of the zoo. However,\n", "your bear truck can only support 300 lbs. Can\n", "you transfer the bears in just one trip?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Bear:\n", " def __init__(self, name, weight):\n", " self.name = name\n", " self.weight = weight\n", " \n", "a = Bear(\"Yogi\", 80)\n", "b = Bear(\"Winnie\", 100)\n", "c = Bear(\"Fozzie\", 115)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "#Class instances in Python can be treated like any other data type:\n", "#they can be assigned to other variables, put in lists, iterated over, etc.\n", "my_bears = [a, b, c]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "total_weight = 0\n", "for z in my_bears:\n", " total_weight += z.weight\n", "print (total_weight < 300)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "total_weight" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#A Zookeeper\u2019s Travails II\n", "Consider now a (marginally) more realistic\n", "scenario, where a bear\u2019s weight changes \n", "when he/she eats and hibernates" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Bear:\n", " def __init__(self, name, weight):\n", " self.name = name\n", " self.weight = weight\n", " def eat(self, amount): \n", " self.weight += amount\n", " def hibernate(self):\n", " self.weight /= 1.20\n", "\n", "a = Bear(\"Yogi\", 80)\n", "b = Bear(\"Winnie\", 100)\n", "c = Bear(\"Fozzie\", 115)\n", "my_bears = [a,b,c]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a.weight" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a.eat(20)\n", "a.weight" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "b.eat(10)\n", "c.hibernate()\n", "c.weight" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "total_weight = 0\n", "for z in my_bears:\n", " total_weight += z.weight\n", "total_weight < 300 " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "total_weight" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }