What is OOP?

Introduzione alla programmazione orientata agli oggetti 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
Introduzione alla programmazione orientata agli oggetti 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

Introduzione alla programmazione orientata agli oggetti 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!
Introduzione alla programmazione orientata agli oggetti 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

Introduzione alla programmazione orientata agli oggetti in Python

Objects in Python

  • Everything in Python is an object
Object Type
5 int
"Hello" str
pd.DataFrame() DataFrame
sum() function
... ...
Introduzione alla programmazione orientata agli oggetti in Python

Classes as blueprints

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

customer class

Introduzione alla programmazione orientata agli oggetti in Python

Classes as blueprints

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

customer class branching out to customer objects

Introduzione alla programmazione orientata agli oggetti 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'>
Introduzione alla programmazione orientata agli oggetti 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
Introduzione alla programmazione orientata agli oggetti 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']
Introduzione alla programmazione orientata agli oggetti 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
Introduzione alla programmazione orientata agli oggetti in Python

Let's review!

Introduzione alla programmazione orientata agli oggetti in Python

Preparing Video For Download...