Commit 5b37c3e5 authored by Yangyang Liu's avatar Yangyang Liu
Browse files

Update Object_Oriented_Programming_Intro.ipynb

parent 3bcad27f
{
type(s){
"cells": [
{
"cell_type": "code",
......@@ -91,34 +91,174 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#Why class:\n",
"#Python is an object-oriented language. This allows us to bundle the properties (data) and behaviors (functions) into individual objects, which make them easy to reuse and build upon.\n",
"#data and functions associated with a specific class are called \"attributes\" and \"methods\", respectively."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.NiceCar object at 0x112360f98>\n",
"<__main__.NiceCar object at 0x112360fd0>\n",
"BMW\n",
"black\n"
]
}
],
"source": [
"# define a class and skip it for now. The class NiceCar has no attributes or methods.\n",
"class NiceCar:\n",
" pass\n",
"\n",
"#instance of a class:\n",
"#a class is a blueprint for creating instances.\n",
"#my_car1and my_car2 are unique instances of class NiceCar\n",
"my_car1 = NiceCar()\n",
"my_car2 = NiceCar()\n",
"\n",
"print(my_car1) #<__main__.NiceCar object at 0x112360208>\n",
"print(my_car2) #<__main__.NiceCar object at 0x112360160>\n",
"\n",
"#instance variables:\n",
"my_car1.brand = 'BMW'\n",
"my_car1.year = '2025'\n",
"my_car1.color = 'black'\n",
"\n",
"my_car2.brand = 'Porsche'\n",
"my_car2.year = '2050'\n",
"my_car2.color = 'red'\n",
"\n",
"print(my_car1.brand) #BMW\n",
"print(my_car2.color) #red"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BMW\n",
"red\n"
]
},
{
"data": {
"text/plain": [
"'Brand: BMW; Purchase Year: 2025 ; Color: black'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#class methods\n",
"#a class method receives the instance as its first argument named by convention \"self\".\n",
"class NiceCar:\n",
" def __init__(self, brand, year, color): #initialize. \n",
" self.brand = brand\n",
" self.year = year\n",
" self.color = color\n",
" \n",
" def info(self):\n",
" output = 'Brand: '+self.brand+'; Purchase Year: '+self.year+'; Color: '+self.color\n",
" return output\n",
"\n",
" \n",
"my_car1 = NiceCar('BMW','2025','black')\n",
"my_car2 = NiceCar('Porsche','2050','red')\n",
"\n",
"print(my_car1.brand) #BMW\n",
"print(my_car2.color) #red\n",
"my_car1.info() #'Brand: BMW; Purchase Year: 2025 ; Color: black'"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Germany\n",
"Germany\n",
"US\n",
"8\n",
"blue-truck\n",
"8\n"
]
},
{
"data": {
"text/plain": [
"'US'"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"class Car(object):\n",
"class Car:\n",
" \n",
" wheels = 4\n",
" #set class attribute\n",
" wheels = 4 \n",
" region = 'Germany'\n",
" \n",
" def __init__(self, color, style):\n",
" self.color = color\n",
" self.style = style\n",
" \n",
" \n",
" #dunder methods (with \"__\") \n",
" def __add__(self, other):\n",
" return self.wheels + other.wheels\n",
" return self.wheels + other.wheels #access class attributes via \"Car.wheels\" or \"self.wheels\"\n",
" \n",
" def __len__(self):\n",
" return(8)\n",
" \n",
" def __str__(self):\n",
" return self.region\n",
" \n",
" def paint(self, new_color):\n",
" self.color = new_color\n",
" \n",
" def plot_something(self):\n",
" return plt.scatter([1,2,3], [1,2,3], color=self.color)\n",
"\n",
"\n",
"my_car = Car(\"blue\", \"truck\")\n",
"other_car = LKW(\"orange\", \"post\")"
"print(my_car) #<__main__.Car object at 0x11db9e5f8>\n",
"\n",
"my_car.region = 'US'\n",
"print(Car.region) #Germany\n",
"print(my_car.region) #US\n",
"\n",
"#dunder method \"add\", \"len\" and \"str\"\n",
"print(my_car+my_car) #8\n",
"print(my_car.color+'-'+my_car.style) #blue-truck\n",
"print(len(my_car)) #8\n",
"print(str(my_car)) #US\n"
]
},
{
......@@ -144,7 +284,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 37,
"metadata": {},
"outputs": [
{
......@@ -153,7 +293,7 @@
"False"
]
},
"execution_count": 51,
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
......@@ -164,7 +304,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 38,
"metadata": {},
"outputs": [
{
......@@ -173,7 +313,7 @@
"'ab'"
]
},
"execution_count": 44,
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
......@@ -184,42 +324,112 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 46,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.LKW object at 0x11eab46d8>\n",
"Germany\n",
"post\n",
"2020\n",
"8\n",
"LKWs can't be printed orange, the company fobids it\n",
"True\n",
"True\n",
"True\n",
"False\n",
"False\n",
"True\n",
"False\n",
"12\n",
"Germany-post\n",
"8\n"
]
}
],
"source": [
"#LKW is a subclass of class Car which inheriates all the functionality (attributes and methods) of the parent class Car.\n",
"class LKW(Car):\n",
" wheels = 8\n",
" wheels = 8 #redefine attributes\n",
" \n",
" #add more arguments based on Car class\n",
" def __init__(self, color, style, brand, year):\n",
" super().__init__(color, style) #inherite the color and style arguments from the parent Car class\n",
" self.brand = brand\n",
" self.year = year\n",
" #redefine paint function for LKW subclass without affecting Car class.\n",
" def paint(self, new_color):\n",
" print(\"LKWs can't be printed, the company fobids it\")"
" print(f\"LKWs can't be printed {new_color}, the company fobids it\")\n",
"\n",
"#print(help(LKW)) #check the attributes and methods of the LKW object\n",
"hermes = LKW(\"orange\", \"post\", \"Mercedes-Benz\", \"2020\")\n",
"print(hermes) #<__main__.LKW object at 0x11ea90cc0>\n",
"print(hermes.region) #Germany\n",
"print(hermes.style) #truck\n",
"print(hermes.year) #2020\n",
"print(hermes.wheels) #8\n",
"hermes.paint(\"orange\") #LKWs can't be printed orange, the company fobids it\n",
"\n",
"\n",
"#Is the object an instance or subclass?\n",
"print(isinstance(my_car, Car)) #True\n",
"print(isinstance(hermes, Car)) #True\n",
"print(isinstance(hermes, LKW)) #True\n",
"print(isinstance(my_car, LKW)) #False\n",
"\n",
"print(issubclass(Car, LKW)) #False\n",
"print(issubclass(LKW, Car)) #True\n",
"print(issubclass(LKW, NiceCar)) #False\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 45,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "TypeError",
"evalue": "can only concatenate str (not \"Car\") to str",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-45-089ca0e86772>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mpaint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mmy_car\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'c'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-45-089ca0e86772>\u001b[0m in \u001b[0;36mpaint\u001b[0;34m(a, b, c)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpaint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mpaint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mmy_car\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'c'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"Car\") to str"
]
}
],
"source": [
"def paint(a, b, c):\n",
" print(a+b+c)"
" return a+b+c\n",
"\n",
"paint('a','b','c')"
]
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LKWs can't be printed, the company fobids it\n"
"ename": "NameError",
"evalue": "name 'hermes' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-22-aa4b3cd705ea>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhermes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpaint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"orange\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'hermes' is not defined"
]
}
],
"source": [
"hermes.paint(\"orange\")\n"
"\n"
]
},
{
......@@ -246,7 +456,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.7.1"
}
},
"nbformat": 4,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment