What is OOP?

Object-Oriented Programming in Python

Alex Yarosh

Content Quality Analyst @ DataCamp

 

Procedural programming

 

  • Code as a sequence of steps
  • Great for data analysis
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

Object-Oriented Programming in Python

 

Procedural programming

 

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

 

Object-oriented programming

 

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

Objects as data structures

$$\Large{\text{Object = state + behavior}}$$

silhouette of a person with a laptop representing a customer

 

$$\text{\textbf{Encapsulation} - bundling data with code operating on it}$$

Object-Oriented Programming in Python

Classes as blueprints

  • Class : blueprint for objects outlining possible states and behaviors

customer class

Object-Oriented Programming in Python

Classes as blueprints

  • Class : blueprint for objects outlining possible states and behaviors

customer class branching out to customer objects

Object-Oriented Programming in Python

Objects in Python

  • Everything in Python is an object
  • Every object has a class
  • Use type() to find the class
import numpy as np
a = np.array([1,2,3,4])
print(type(a))
numpy.ndarray
Object Class
5 int
"Hello" str
pd.DataFrame() DataFrame
np.mean function
... ...
Object-Oriented Programming in Python

Attributes and methods

State ↔ attributes
import numpy as np
a = np.array([1,2,3,4])

# shape attribute a.shape
(4,)

 

  • Use obj. to access attributes and methods
Behavior ↔ methods
import numpy as np
a = np.array([1,2,3,4])

# reshape method a.reshape(2,2)
array([[1, 2],
       [3, 4]])
Object-Oriented Programming in Python

Object = attributes + methods

  • attribute ↔ variablesobj.my_attribute,

  • method ↔ function()obj.my_method().

import numpy as np
a = np.array([1,2,3,4])
dir(a)                # <--- list all attributes and methods
['T',
 '__abs__',
 ...
 'trace',
 'transpose',
 'var',
 'view']

Object-Oriented Programming in Python

Let's review!

Object-Oriented Programming in Python

Preparing Video For Download...