Inheritance : Is - a Vers u s Has - A W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON Vicki Bo y kis Senior Data Scientist
E x tending o u r DataShells Inheritance - A class that takes on a � rib u tes from another , " parent " class and adds some more of its o w n f u nctionalit y. WORKING WITH THE CLASS SYSTEM IN PYTHON
T w o Dinosa u rs WORKING WITH THE CLASS SYSTEM IN PYTHON
Is a and Has a Relationship WORKING WITH THE CLASS SYSTEM IN PYTHON
Is a and Has a Relationship A Pterodact y l is - a Dinosa u r A T y rannosa u r u s is - a Dinosa u r Is a Pterodact y l a dinosa u r ? Yes , pterodact y l inherits from dinosa u r . Is a T y rannosa u r u s a pterodact y l ? No , b u t the y' re both dinosa u rs . Is a dinosa u r a pterodact y l ? No , so it doesn ' t w ork the other w a y, either . WORKING WITH THE CLASS SYSTEM IN PYTHON
Inheriting a DataShell # new class which inherits from DataShell: StDevDataShell class StDevDataShell(DataShell): # now this class inherits all attributes and methods from DataShell pass WORKING WITH THE CLASS SYSTEM IN PYTHON
Let ' s practice ! W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON
Inheritance w ith DataShells W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON Vicki Bo y kis Sr . Data Scientist
DataShell w ith Standard De v iation WORKING WITH THE CLASS SYSTEM IN PYTHON
Changing the DataShell class DataShell: def __init__(self, filename): self.filename = filename def create_datashell(self): data_array = np.genfromtxt(self.filename, delimiter=',', dtype=None) self.array = data_array return self.array def show_shell(self): print(self.array) def rename_column(self, old_colname, new_colname): for index, value in enumerate(self.array[0]): if value == old_colname.encode('UTF-8'): self.array[0][index] = new_colname return self.array def five figure summary(self col position): WORKING WITH THE CLASS SYSTEM IN PYTHON
Allo w ing for a standard de v iation def get_stdev(self,col_position): column = self.array[1:,col_position].astype(np.float) stdev = np.ndarray.std(column,axis=0) return f"Standard Deviation of column {col_position}: {stdev}" WORKING WITH THE CLASS SYSTEM IN PYTHON
Inheritance w ith DataShells class DataStDev(DataShell): def __init__(self,filename): DataShell.filename = filename def get_stdev(self,col_position): column = self.array[1:,col_position].astype(np.float) stdev = np.ndarray.std(column,axis=0) return f"Standard Deviation of column {col_position}: {stdev}" WORKING WITH THE CLASS SYSTEM IN PYTHON
Calling o u r ne w DataShell Code to call it : car_data = 'mtcars.csv' my_st_dev_shell = DataStDev(car_data) my_st_dev_shell.create_datashell() my_st_dev_shell.get_stdev(1) 'Standard Deviation of column 1: 5.932029552301218' WORKING WITH THE CLASS SYSTEM IN PYTHON
Let ' s practice ! W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON
Composition W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON Vicki Bo y kis Senior Data Scientist
Inheritance v ers u s Composition WORKING WITH THE CLASS SYSTEM IN PYTHON
Composing w ith Animals WORKING WITH THE CLASS SYSTEM IN PYTHON
Composition In a DataShell - 1 Fi v e - Fig u re S u mmar y Composition def five_figure_summary(self,col_position): statistics = stats.describe(self.array[1:,col_position].astype(np.float)) return f"Five-figure stats of column {col_position}: {statistics}" WORKING WITH THE CLASS SYSTEM IN PYTHON
Composition In a DataShell - 2 Create DataShell Composition : def create_datashell(self): data_array = np.genfromtxt(self.filename, delimiter=',', dtype=None) self.array = data_array return self.array WORKING WITH THE CLASS SYSTEM IN PYTHON
Composing w ith Pandas Create DataShell Composition : class DataShell: def __init__(self, filename): self.filename = filename def create_datashell(self): data_array = np.genfromtxt(self.filename, delimiter=',', dtype=None) self.array = data_array return self.array class DataShellComposed: def __init__(self, filename): self.filename = filename def create_datashell(self): self.df = pandas.read_csv() return self.df WORKING WITH THE CLASS SYSTEM IN PYTHON
What does o u r ne w class look like ? car_data = 'mtcars.csv' my_data_shell = DataShellComposed(car_data) my_data_shell.create_datashell() print(type(my_data_shell.df)) <class 'pandas.core.frame.DataFrame'> WORKING WITH THE CLASS SYSTEM IN PYTHON
Let ' s practice ! W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON
Wrapping Up OOP W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON Vicki Bo y kis Senior Data Scientist
Understanding Objects WORKING WITH THE CLASS SYSTEM IN PYTHON
Understanding Classes and Instances of Classes . Classes : Made u p of methods and a � rib u tes Initiali z ed w ith an init constr u ctor method Has a self a � rib u te that ' s referring to the class ( or partic u lar instance of that class ) WORKING WITH THE CLASS SYSTEM IN PYTHON
The DataShell WORKING WITH THE CLASS SYSTEM IN PYTHON
The benefits of OOP WORKING WITH THE CLASS SYSTEM IN PYTHON
Inheritance and Composition WORKING WITH THE CLASS SYSTEM IN PYTHON
Ho w to get better at OOP 1) Read w ell - doc u mented codebases 2) Write y o u r o w n classes 3) Don ' t get disco u raged ! WORKING WITH THE CLASS SYSTEM IN PYTHON
Final Steps W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON
More recommend