Created
November 4, 2013 15:27
-
-
Save yeesian/7304207 to your computer and use it in GitHub Desktop.
OOP Review
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Agenda\n", | |
"---\n", | |
"\n", | |
"- Recap: Message Passing\n", | |
"- Object-Oriented Programming (OOP)\n", | |
"- Inheritance\n", | |
"- Polymorphism" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Recap: Message-Passing\n", | |
"---\n", | |
"\n", | |
"In message passing, it is the data that is \u201cintelligent\u201d:\n", | |
"\n", | |
"- They know how to act on themselves\n", | |
"- You just \"tell\" the data what you want done\n", | |
"- Pass messages to the object" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def make_rect(x,y):\n", | |
" def helper(msg):\n", | |
" if msg == \"get_real\":\n", | |
" return x\n", | |
" elif msg == \"get_imag\":\n", | |
" return y\n", | |
" elif msg == \"angle\":\n", | |
" return math.atan(y/x)\n", | |
" elif msg == \"magnitude\":\n", | |
" return math.sqrt(x*x+y*y)\n", | |
" else:\n", | |
" raise Exception(\"Invalid method!\")\n", | |
" return helper\n", | |
"\n", | |
"def make_polar(r,a):\n", | |
" def helper(msg):\n", | |
" if msg == \"get_real\":\n", | |
" return r*math.cos(a)\n", | |
" elif msg == \"get_imag\":\n", | |
" return r*math.sin(a)\n", | |
" elif msg == \"angle\":\n", | |
" return a\n", | |
" elif msg == \"magnitude\":\n", | |
" return r\n", | |
" else:\n", | |
" raise Exception(\"Invalid method!\")\n", | |
" return helper" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"z1 = make_rect(1,1)\n", | |
"print(z1(\"get_real\"))\n", | |
"print(z1(\"get_imag\"))\n", | |
"print(z1(\"angle\"))\n", | |
"print(z1(\"magnitude\"))\n", | |
"z2 = make_rect(1,0)\n", | |
"print(z2(\"get_real\"))\n", | |
"print(z2(\"get_imag\"))\n", | |
"print(z2(\"angle\"))\n", | |
"print(z2(\"magnitude\"))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1\n", | |
"1\n", | |
"0.785398163397\n", | |
"1.41421356237\n", | |
"1\n", | |
"0\n", | |
"0.0\n", | |
"1.0\n" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Recap: Message Passing\n", | |
"---\n", | |
"\n", | |
"- Effectively, each data object stores its own lookup table!\n", | |
"- Match function to representation:\n", | |
" - Dispatch on Type (if-else)\n", | |
" - Data-Directed Programming (lookup table)\n", | |
" - Message Passing (function in data)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Object-Oriented Programming\n", | |
"---\n", | |
"Combines two powerful computational ideas:\n", | |
"\n", | |
"1. Generic operations (\u201cmessage passing\u201d)\n", | |
"2. Object-based abstractions\n", | |
"\n", | |
"Major concepts:\n", | |
"\n", | |
"- Classes and instances\n", | |
"- Methods and message passing\n", | |
"- Inheritance\n", | |
"- Polymorphism" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class BankAccount(object):\n", | |
" def __init__(self, initial_balance): # Constructor (recall: data abstraction?)\n", | |
" self.balance = initial_balance\n", | |
"\n", | |
" def withdraw(self, amount): # Actions\n", | |
" if self.balance > amount:\n", | |
" self.balance -= amount\n", | |
" return self.balance\n", | |
" else:\n", | |
" return \"Money not enough\"\n", | |
" \n", | |
" def deposit(self, amount):\n", | |
" self.balance += amount\n", | |
" return self.balance\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The code above defines a **class**, whereas in the example below, we're creating an **instance** (`ben_account`) of the class `BankAccount`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
">>> ben_account = BankAccount(100)\n", | |
">>> ben_account.withdraw(40) # 60\n", | |
">>> ben_account.withdraw(200) # \"Money not enough\"\n", | |
">>> ben_account.deposit(20) #80" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 15, | |
"text": [ | |
"80" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Inheritance\n", | |
"---\n", | |
"\n", | |
"- Objects that exhibit similar functionality should \u201cinherit\u201d from the same base object, called the **superclass**.\n", | |
"- An object that inherits from another is called the **subclass**\n", | |
"- Superclass vs Subclass\n", | |
" - Subclass specializes the superclass by extending state/behavior\n", | |
"- Classes have an \u201cis-a\u201d relationship with their superclasses (establishes a type hierarchy)\n", | |
"\n", | |
"**Remark**: Attempt and understand Mission 14 to brush up on your understanding!" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Do you notice repeated code?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class NamedObject(object):\n", | |
" def __init__ (self, name):\n", | |
" self.name = name # repeated\n", | |
"\n", | |
"\n", | |
"class MobileObject(NamedObject):\n", | |
" def __init__ (self, name, location):\n", | |
" self.name = name # repeated\n", | |
" self.location = location" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The \u2018super()\u2019 method" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class NamedObject(object):\n", | |
" def __init__(self, name):\n", | |
" self.name = name.lower()\n", | |
"\n", | |
"\n", | |
"class MobileObject(NamedObject):\n", | |
" def __init__(self, name, location):\n", | |
" super().__init__(name)\n", | |
" self.location = location\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Another Example (slides 64 - 72)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Speaker(object):\n", | |
" def say(self,stuff):\n", | |
" print(stuff)\n", | |
"\n", | |
"class Lecturer(Speaker):\n", | |
"\tdef lecture(self, stuff):\n", | |
"\t\tself.say(stuff) # whatever a speaker is supposed to do\n", | |
"\t\tself.say(\"You should be taking notes\") # Extends the behavior of a speaker\n", | |
" \n", | |
"class ArrogantLecturer(Lecturer):\n", | |
"\tdef __init__(self, favourite_phrase):\n", | |
"\t\tself.favourite_phrase = favourite_phrase\n", | |
"\n", | |
"\tdef say(self, stuff):\n", | |
"\t\tsuper().say(stuff + self.favourite_phrase) # what is super() here referring to?" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"I'll skip through Polymorphism ..." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Multiple Inheritance\n", | |
"---" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Singer(object):\n", | |
"\tdef say(self, stuff):\n", | |
"\t\tprint(\"tra-la-la -- \" + stuff)\n", | |
"\n", | |
"\tdef sing(self):\n", | |
"\t\tprint(\"tra-la-la\")\n", | |
"\n", | |
"class SingingArrogantLecturer(ArrogantLecturer, Singer): # what are the superclasses here? 2 of them!\n", | |
"\tdef __init__(self, favourite_phrase):\n", | |
"\t\tsuper().__init__(favourite_phrase)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 29 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment