Commit 4c4df7a7 authored by Yangyang Liu's avatar Yangyang Liu
Browse files

Replace Object_Oriented_Programming_Intro.ipynb

parent 3bcad27f
%% Cell type:code id: tags:
``` python
s = "trees"
```
%% Cell type:code id: tags:
``` python
type(s)
```
%% Output
str
%% Cell type:code id: tags:
``` python
import numpy as np
a = np.zeros((5, 5))
```
%% Cell type:code id: tags:
``` python
# Look up how to determine callable
```
%% Output
<function len(obj, /)>
%% Cell type:code id: tags:
``` python
s.capitalize()
```
%% Output
'Trees'
%% Cell type:code id: tags:
``` python
def func_name(args, kwargs):
pass
```
%% Cell type:code id: tags:
``` python
#Why class:
#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.
#data and functions associated with a specific class are called "attributes" and "methods", respectively.
```
%% Cell type:code id: tags:
``` python
# define a class and skip it for now. The class NiceCar has no attributes or methods.
class NiceCar:
pass
#instance of a class:
#a class is a blueprint for creating instances.
#my_car1and my_car2 are unique instances of class NiceCar
my_car1 = NiceCar()
my_car2 = NiceCar()
print(my_car1) #<__main__.NiceCar object at 0x112360208>
print(my_car2) #<__main__.NiceCar object at 0x112360160>
#instance variables:
my_car1.brand = 'BMW'
my_car1.year = '2025'
my_car1.color = 'black'
my_car2.brand = 'Porsche'
my_car2.year = '2050'
my_car2.color = 'red'
print(my_car1.brand) #BMW
print(my_car2.color) #red
```
%% Output
<__main__.NiceCar object at 0x112360f98>
<__main__.NiceCar object at 0x112360fd0>
BMW
black
%% Cell type:code id: tags:
``` python
#class methods
#a class method receives the instance as its first argument named by convention "self".
class NiceCar:
def __init__(self, brand, year, color): #initialize.
self.brand = brand
self.year = year
self.color = color
def info(self):
output = 'Brand: '+self.brand+'; Purchase Year: '+self.year+'; Color: '+self.color
return output
my_car1 = NiceCar('BMW','2025','black')
my_car2 = NiceCar('Porsche','2050','red')
print(my_car1.brand) #BMW
print(my_car2.color) #red
my_car1.info() #'Brand: BMW; Purchase Year: 2025 ; Color: black'
```
%% Output
BMW
red
'Brand: BMW; Purchase Year: 2025 ; Color: black'
%% Cell type:code id: tags:
``` python
import matplotlib.pyplot as plt
class Car(object):
class Car:
#set class attribute
wheels = 4
region = 'Germany'
def __init__(self, color, style):
self.color = color
self.style = style
#dunder methods (with "__")
def __add__(self, other):
return self.wheels + other.wheels
return self.wheels + other.wheels #access class attributes via "Car.wheels" or "self.wheels"
def __len__(self):
return(8)
def __str__(self):
return self.region
def paint(self, new_color):
self.color = new_color
def plot_something(self):
return plt.scatter([1,2,3], [1,2,3], color=self.color)
my_car = Car("blue", "truck")
other_car = LKW("orange", "post")
print(my_car) #<__main__.Car object at 0x11db9e5f8>
my_car.region = 'US'
print(Car.region) #Germany
print(my_car.region) #US
#dunder method "add", "len" and "str"
print(my_car+my_car) #8
print(my_car.color+'-'+my_car.style) #blue-truck
print(len(my_car)) #8
print(str(my_car)) #US
```
%% Output
Germany
Germany
US
8
blue-truck
8
'US'
%% Cell type:code id: tags:
``` python
int(my_car)
```
%% Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-54-f5d0a29c858f> in <module>
----> 1 int(my_car)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Car'
%% Cell type:code id: tags:
``` python
"abc" > "d"
```
%% Output
False
%% Cell type:code id: tags:
``` python
"a" + "b"
```
%% Output
'ab'
%% Cell type:code id: tags:
``` python
#LKW is a subclass of class Car which inheriates all the functionality (attributes and methods) of the parent class Car.
class LKW(Car):
wheels = 8
wheels = 8 #redefine attributes
#add more arguments based on Car class
def __init__(self, color, style, brand, year):
super().__init__(color, style) #inherite the color and style arguments from the parent Car class
self.brand = brand
self.year = year
#redefine paint function for LKW subclass without affecting Car class.
def paint(self, new_color):
print("LKWs can't be printed, the company fobids it")
print(f"LKWs can't be printed {new_color}, the company fobids it")
#print(help(LKW)) #check the attributes and methods of the LKW object
hermes = LKW("orange", "post", "Mercedes-Benz", "2020")
print(hermes) #<__main__.LKW object at 0x11ea90cc0>
print(hermes.region) #Germany
print(hermes.style) #truck
print(hermes.year) #2020
print(hermes.wheels) #8
hermes.paint("orange") #LKWs can't be printed orange, the company fobids it
#Is the object an instance or subclass?
print(isinstance(my_car, Car)) #True
print(isinstance(hermes, Car)) #True
print(isinstance(hermes, LKW)) #True
print(isinstance(my_car, LKW)) #False
print(issubclass(Car, LKW)) #False
print(issubclass(LKW, Car)) #True
print(issubclass(LKW, NiceCar)) #False
```
%% Output
<__main__.LKW object at 0x11eab46d8>
Germany
post
2020
8
LKWs can't be printed orange, the company fobids it
True
True
True
False
False
True
False
12
Germany-post
8
%% Cell type:code id: tags:
``` python
def paint(a, b, c):
print(a+b+c)
return a+b+c
paint('a','b','c')
```
%% Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-45-089ca0e86772> in <module>
2 return a+b+c
3
----> 4 paint('a',my_car,'c')
<ipython-input-45-089ca0e86772> in paint(a, b, c)
1 def paint(a, b, c):
----> 2 return a+b+c
3
4 paint('a',my_car,'c')
TypeError: can only concatenate str (not "Car") to str
%% Cell type:code id: tags:
``` python
hermes.paint("orange")
```
%% Output
LKWs can't be printed, the company fobids it
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-22-aa4b3cd705ea> in <module>
----> 1 hermes.paint("orange")
NameError: name 'hermes' is not defined
%% Cell type:code id: tags:
``` python
```
......
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