0% found this document useful (0 votes)
35 views29 pages

Working With The Class System in Python Chapter4

This document discusses object oriented programming concepts in Python including inheritance, composition, and classes and instances. It uses a DataShell class to demonstrate inheritance by creating a subclass called DataStDev that inherits from DataShell and allows calculating standard deviation. Composition is demonstrated by creating subclasses that utilize other classes as components rather than inheriting from them. The benefits of OOP such as code reuse and organization are discussed. Tips for improving OOP skills are provided.

Uploaded by

Fgpeqw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views29 pages

Working With The Class System in Python Chapter4

This document discusses object oriented programming concepts in Python including inheritance, composition, and classes and instances. It uses a DataShell class to demonstrate inheritance by creating a subclass called DataStDev that inherits from DataShell and allows calculating standard deviation. Composition is demonstrated by creating subclasses that utilize other classes as components rather than inheriting from them. The benefits of OOP such as code reuse and organization are discussed. Tips for improving OOP skills are provided.

Uploaded by

Fgpeqw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

DataCamp Object Oriented Programming in Python

OBJECT ORIENTED PROGRAMMING IN PYTHON

Inheritance: Is-a Versus


Has-A

Vicki Boykis
Senior Data Scientist
DataCamp Object Oriented Programming in Python

Extending our DataShells

Inheritance - A class that takes on attributes from another, "parent" class and adds
some more of its own functionality.
DataCamp Object Oriented Programming in Python

Two Dinosaurs
DataCamp Object Oriented Programming in Python

Is a and Has a Relationship


A Pterodactyl is-a Dinosaur
A Tyrannosaurus is-a Dinosaur
Is a Pterodactyl a dinosaur? Yes,pterodactyl inherits from dinosaur.
Is a Tyranosaurus a pterodactyl? No, but they're both dinosaurs.
Is a dinosaur a pterodactyl? No, so it doesn't work the other way, either.
DataCamp Object Oriented Programming in Python

Inheriting a DataShell
# the class we want to create
StDevDataShell

class StDevDataShell(DataShell):
pass
DataCamp Object Oriented Programming in Python

OBJECT ORIENTED PROGRAMMING IN PYTHON

Let's practice!
DataCamp Object Oriented Programming in Python

OBJECT ORIENTED PROGRAMMING IN PYTHON

Inheritance with DataShells

Vicki Boykis
Sr. Data Scientist
DataCamp Object Oriented Programming in Python

DataShell with Standard Deviation


DataCamp Object Oriented Programming 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):
statistics = stats.describe(self.array[1:,col_position]
.astype(np.float))
return f"Five-figure stats of column {col_position}: {statistics}"
DataCamp Object Oriented Programming in Python

Allowing for a standard deviation


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}"
DataCamp Object Oriented Programming in Python

Inheritance with 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}"
DataCamp Object Oriented Programming in Python

Calling our new DataShell

Code to call it:

carData = 'mtcars.csv'

myStDevShell = DataStDev(carData)
myStDevShell.create_datashell()
myStDevShell.get_stdev(1)
'Standard Deviation of column 1: 5.932029552301218'
DataCamp Object Oriented Programming in Python

OBJECT ORIENTED PROGRAMMING IN PYTHON

Let's practice!
DataCamp Object Oriented Programming in Python

OBJECT ORIENTED PROGRAMMING IN PYTHON

Composition

Vicki Boykis
Senior Data Scientist
DataCamp Object Oriented Programming in Python

Inheritance versus Composition


DataCamp Object Oriented Programming in Python

Composing with Animals


DataCamp Object Oriented Programming in Python

Composition In a DataShell - 1

Five-Figure Summary 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}"
DataCamp Object Oriented Programming 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
DataCamp Object Oriented Programming in Python

Composing with 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
DataCamp Object Oriented Programming in Python

What does our new class look like?


carData = 'mtcars.csv'
myDatashell = DataShellComposed(carData)
myDatashell.create_datashell()
print(type(myDatashell.df))
<class 'pandas.core.frame.DataFrame'>
DataCamp Object Oriented Programming in Python

OBJECT ORIENTED PROGRAMMING IN PYTHON

Let's practice!
DataCamp Object Oriented Programming in Python

OBJECT ORIENTED PROGRAMMING IN PYTHON

Wrapping Up OOP

Vicki Boykis
Senior Data Scientist
DataCamp Object Oriented Programming in Python

Understanding Objects
DataCamp Object Oriented Programming in Python

Understanding Classes and Instances of Classes.

Classes:

Made up of methods and attributes


Initialized with an init constructor method
Has a self attribute that's referring to the class (or particular instance of that class)
DataCamp Object Oriented Programming in Python

The DataShell
DataCamp Object Oriented Programming in Python

The benefits of OOP


DataCamp Object Oriented Programming in Python

Inheritance and Composition


DataCamp Object Oriented Programming in Python

How to get better at OOP

1) Read well-documented codebases

2) Write your own classes

3) Ask for feedback

4) Don't get discouraged!


DataCamp Object Oriented Programming in Python

OBJECT ORIENTED PROGRAMMING IN PYTHON

Final Steps

You might also like