Skip to content

Instantly share code, notes, and snippets.

@Jessime
Last active August 29, 2015 14:26
Show Gist options
  • Save Jessime/fc2654fd3b0e8b9aa3fb to your computer and use it in GitHub Desktop.
Save Jessime/fc2654fd3b0e8b9aa3fb to your computer and use it in GitHub Desktop.
Object Oriented Programming
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#Object Oriented Programming (OOP)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Goals:\n",
"* Benefits\n",
"* Terminology\n",
"* Examples"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Object oriented programming isn't often referred to as a tool. In languages like Java, every line of code you write is part of an object. "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Two good reasons to squeeze it in anyway:\n",
"* It's extremely common\n",
"* It's just a tool"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"In higher-level languages like Python, you only have to write objects when it's beneficial."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"##Benefits "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"**Flexibility**"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"OOP uses very modular code you can recombine in differnt ways."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"**Documentation**"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Besides encouraging explicit documentation, OOP creates self-documenting code."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"**Organization**"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Your data and functions are necessarily organized."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Terminology"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"These are a few of the programming terms used in OOP."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"**Class**"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"A class is the code definition of some entity. People or DNA could be classes, for example."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"**Object**"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"An object is a specific instance, or member, of a class. You could be an object of a person class."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"**Attribute**"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"All of the data defining an object is stored in its attributes. \"Height\" is an attribute of a person. "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"**Method**"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Methods are specific functions that can be preformed by a class. \"Eat\" might be a person method."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#Example "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"While classes can be abstract virtual things like a DataFrame, it's easier to start thinking about objects as real things. Let's define a person and go through a light hearted example."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"class Person():\n",
" \"\"\"A generic person which can be used for experimentation.\"\"\"\n",
" def __init__(self, height, weight, bmi, iq):\n",
" self.height = height\n",
" self.weight = weight\n",
" self.bmi = bmi\n",
" self.iq = iq"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"So far we have defined a Person class and defined its __attributes__. We can play with this class a little bit to see how the \"\\_\\_init\\_\\_\" method works."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"jerry = Person(65, 120, 2, 120)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Jerry is an __object__ or instance."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'A generic person which can be used for experimentation.'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"jerry.__doc__"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"65\n",
"120\n"
]
}
],
"source": [
"print jerry.height\n",
"print jerry.weight"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Is that BMI actually right? Since we're already passing height and weight, let's add a __method__ to save some trouble."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"class Person():\n",
" \"\"\"A generic person which can be used for experimentation.\"\"\"\n",
" def __init__(self, height, weight, iq):\n",
" self.height = float(height)\n",
" self.weight = float(weight)\n",
" self.bmi = self.get_bmi()\n",
" self.iq = iq\n",
" \n",
" def get_bmi(self):\n",
" \"\"\"Calculates a person's body mass index\"\"\"\n",
" return (703*self.weight)/(self.height**2)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"jerry = Person(65, 120, 120)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"65.0\n",
"120.0\n",
"19.9668639053\n"
]
}
],
"source": [
"print jerry.height\n",
"print jerry.weight\n",
"print jerry.bmi"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Great, Jerry isn't dead; it was just a typo."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"What are some other __methods__ and __attributes__ a person class could contain?"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"class Person():\n",
" \"\"\"A generic person which can be used for experimentation.\"\"\"\n",
" def __init__(self, height, weight, iq, name):\n",
" self.height = float(height)\n",
" self.weight = float(weight)\n",
" self.bmi = self.get_bmi()\n",
" self.iq = iq\n",
" self.name = name\n",
" self.friends = []\n",
" self.likeability = np.random.rand()\n",
" \n",
" def get_bmi(self):\n",
" \"\"\"Calculates a person's body mass index\"\"\"\n",
" return (703*self.weight)/(self.height**2)\n",
" \n",
" def study(self):\n",
" \"\"\"A simple incrementer for a person's IQ\"\"\"\n",
" self.iq += 1\n",
" \n",
" def make_friend(self, new_friend):\n",
" \"\"\"Try adding new friends to a person's friends list.\n",
" If the person isn't friendly enough, increase their likeability.\"\"\"\n",
" if self.likeability > .5 and new_friend not in self.friends:\n",
" self.friends.append(new_friend)\n",
" elif new_friend in self.friends:\n",
" print \"{} is already friends with {}.\".format(new_friend.name, self.name)\n",
" else:\n",
" print \"This person isn't likeable enough ({}) for friends.\".format(self.likeability)\n",
" self.likeability += .1\n",
" \n",
" def hello2friends(self):\n",
" \"\"\"Say hello to all of the people this person is friends with.\"\"\"\n",
" for pal in self.friends:\n",
" print \"Hello {}!\".format(pal.name)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Things start to get interesting when you make multiple instances of a class. "
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"jerry = Person(65, 120, 120, \"Jerry\")"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"sally = Person(60, 100, 150, \"Sally\")"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"fred = Person(71, 210, 145, \"Fred\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Fred just met Sally and Jerry, and want's to become their friend."
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This person isn't likeable enough (0.251861686928) for friends.\n"
]
}
],
"source": [
"fred.make_friend(jerry)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Unfortunately, it looks like Fred's a bit of a jerk. Fortunately, he's persistant, and practice makes perfect:"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This person isn't likeable enough (0.351861686928) for friends.\n",
"This person isn't likeable enough (0.451861686928) for friends.\n",
"Jerry is already friends with Fred.\n"
]
}
],
"source": [
"for i in xrange(4):\n",
" fred.make_friend(jerry)\n",
"fred.make_friend(sally)"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[<__main__.Person instance at 0x00000000036FD908>, <__main__.Person instance at 0x0000000003CA99C8>]\n"
]
}
],
"source": [
"print fred.friends"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello Jerry!\n",
"Hello Sally!\n"
]
}
],
"source": [
"fred.hello2friends()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Again, one of the benefits of OOP is flexibility. We can use these fairly complicated people to do something completely different than what we just did. The following is slightly more serious, but still completely contrived. We're going to test if scientist are smarter than a control group."
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"90\n"
]
}
],
"source": [
"names = ['John', 'William', 'James', 'Charles', 'George', 'Frank', 'Joseph', \n",
"'Thomas', 'Henry', 'Robert', 'Edward', 'Harry', 'Walter', 'Arthur', 'Fred',\n",
" 'Albert', 'Samuel', 'David', 'Louis', 'Joe', 'Charlie', 'Clarence', 'Richard',\n",
" 'Andrew', 'Daniel', 'Ernest', 'Will', 'Jesse', 'Oscar', 'Lewis', 'Peter', \n",
" 'Benjamin', 'Frederick', 'Willie', 'Alfred', 'Sam', 'Roy', 'Herbert', 'Jacob', \n",
" 'Tom', 'Elmer', 'Carl', 'Lee', 'Howard', 'Martin', 'Michael', 'Bert', \n",
" 'Herman', 'Jim', 'Francis', 'Harvey', 'Earl', 'Eugene', 'Ralph', 'Ed', \n",
" 'Claude', 'Edwin', 'Ben', 'Charley', 'Paul', 'Edgar', 'Isaac', 'Otto', \n",
" 'Luther', 'Lawrence', 'Ira', 'Patrick', 'Guy', 'Oliver', 'Theodore', 'Hugh',\n",
" 'Clyde', 'Alexander', 'August', 'Floyd', 'Homer', 'Jack', 'Leonard', 'Horace', \n",
" 'Marion', 'Philip', 'Allen', 'Archie', 'Stephen', 'Chester', 'Willis', \n",
" 'Raymond', 'Rufus', 'Warren', 'Dan']\n",
"print len(names)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"heights = np.random.randint(48, 75, len(names))\n",
"weights = np.random.randint(130, 250, len(names))\n",
"iqs = np.random.randint(80, 130, len(names))\n",
"attributes = zip(heights, weights, iqs, names)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"scientists = [Person(h, w, i, n) for h,w,i,n in attributes[:45]]"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"control = [Person(h, w, i, n) for h,w,i,n in attributes[45:]]"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"45\n",
"45\n"
]
}
],
"source": [
"print len(scientists)\n",
"print len(control)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Let's simulate graduate school..."
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"for i in xrange(6):\n",
" for grad in scientists:\n",
" grad.study()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Let's do a simple statistical test to see if graduate students are normal after graduate school:"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Scientists are probably different: 0.00096045033007\n"
]
}
],
"source": [
"from scipy.stats import ks_2samp\n",
"sci_iq = [grad.iq for grad in scientists]\n",
"ctrl_iq = [c.iq for c in control]\n",
"p_val = ks_2samp(sci_iq, ctrl_iq)[1]\n",
"if p_val > .05:\n",
" print \"Scientists may be normal: \", p_val\n",
"else:\n",
" print \"Scientists are probably different: \", p_val"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment