What is OOP?

Introduction to Object-Oriented Programming in Python

George Boorman

Curriculum Manager, DataCamp

Procedural programming

 

  • Code as a sequence of steps
  • Great for data analysis

Stairs

1 Image source: https://unsplash.com/@tateisimikito
Introduction to Object-Oriented Programming in Python

Thinking in sequences

 

icons of a person in bed followed by a coffee icon followed by a briefcase icon

 

an assortment of icons representing many activities

Introduction to Object-Oriented Programming in Python

Procedural programming

 

  • Code as a sequence of steps
  • Great for data analysis

Object-oriented programming

 

  • Code as interactions of objects
  • Great for building software
  • Maintainable and reusable code!
Introduction to Object-Oriented Programming in Python

Objects

$$\Large{\text{Object = data + functionality}}$$

silhouette of a person with a laptop representing a customer

                                                          State - an object's data

                                                  Behavior - an object's functionality

Introduction to Object-Oriented Programming in Python

Objects in Python

  • Everything in Python is an object
Object Type
5 int
"Hello" str
pd.DataFrame() DataFrame
sum() function
... ...
Introduction to Object-Oriented Programming in Python

Classes as blueprints

  • Class: a blueprint for objects outlining possible states and behaviors

customer class

Introduction to Object-Oriented Programming in Python

Classes as blueprints

  • Class : a blueprint for objects outlining possible states and behaviors

customer class branching out to customer objects

Introduction to Object-Oriented Programming in Python

Classes in Python

  • Python objects of the same type behave in the same way
  • lists are a class
    • Created with comma-separated values [1, 2, 3, 4, 5]
    • Share the same methods, e.g., .append()
  • Use type() to find the class
type([1, 2, 3, 4, 5])
<class 'list'>
Introduction to Object-Oriented Programming in Python

Attributes and methods

State ↔ attributes
import pandas as pd
df = pd.DataFrame({"a": [1,2,3], 
                   "b": [4,5,6]})

# shape attribute df.shape
(3, 2)
  • Use obj. to access attributes and methods
Behavior ↔ methods
import pandas as pd
df = pd.DataFrame({"a": [1,2,3], 
                   "b": [4,5,6]})

# head method df.head()
   a  b
0  1  4
1  2  5
2  3  6
Introduction to Object-Oriented Programming in Python

Displaying attributes and methods

# Display attributes and methods
dir([1, 2, 3, 4])
['__add__',
 '__class__',
 '__contains__',
 '__delattr__', 
 ...
 'pop',
 'remove',
 'reverse',
 'sort']
# Display attributes and methods
dir(list)
['__add__',
 '__class__',
 '__contains__',
 '__delattr__', 
 ...
 'pop',
 'remove',
 'reverse',
 'sort']
Introduction to Object-Oriented Programming in Python

Cheat sheet

Term Definition
Class A blueprint/template used to build objects
Object A combination of data and functionality; An instance of a class
State Data associated with an object, assigned through attributes
Behavior An object's functionality, defined through methods
Introduction to Object-Oriented Programming in Python

Let's review!

Introduction to Object-Oriented Programming in Python

Preparing Video For Download...