2. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
● Part I: Object Oriented Programming
○ What is OOP?
○ Logical Example
○ Attributes and methods
○ Why to use objects
○ Defining objects
● Part II: Improving your code
○ Extending objects
○ Lambdas
○ Comprehensions
○ Iterables
○ Properties
Outline
● Part III: Exceptions
○ What are exceptions?
○ Handling exceptions
○ Raising exceptions
○ Creating custom exceptions
● Part IV: logging and testing
○ The Python logging module
○ Basics about testing
○ The Python unit-testing module
○ Test-driven development
3. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
● Part I: Object Oriented Programming
○ What is OOP?
○ Logical Example
○ Attributes and methods
○ Why to use objects
○ Defining objects
● Part II: Improving your code
○ Extending objects
○ Lambdas
○ Comprehensions
○ Iterables
○ Properties
Outline
● Part III: Exceptions
○ What are exceptions?
○ Handling exceptions
○ Raising exceptions
○ Creating custom exceptions
● Part IV: logging and testing
○ The Python logging module
○ Basics about testing
○ The Python unit-testing module
○ Test-driven development
4. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
It is a programming paradigm. Things change quite a lot form “classic”
programming. Objects are “entities” which model the world around us.
Objects are defined as classes
Object Oriented Programming
→ What is it?
5. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Living organisms
Reptiles
Mammals
Bipeds Quadrupeds
Object Oriented Programming
→ What is it?
6. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Glasses
Alcoholic Content
Alcohol Free
Content
Wine Liquor
White
Red
Coffee Beverages
Espresso American
Object Oriented Programming
→ What is it?
7. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Glasses
Glass Plastic
Coloured Transparent
Painted
Compound
Recyclable
Not
Recyclable
Object Oriented Programming
→ What is it?
8. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
It is a programming paradigm. Things change quite a lot form “classic”
programming. Objects are “entities” which model the world around us.
Objects are defined as classes.
To use objects, we need to create an instance of their class.
Objects can have:
- attributes (variables)
- methods (functions)
Object Oriented Programming
→ What is it?
9. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Person Class
- name
- say_hi()
print('Hello!')
Person Class instance
- name = Mario
- say_hi()
print('Hello!')
Instantiation*
Object Oriented Programming
→ Logical Example
*Also known as construction or initialization
10. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Person Class
- name
- say_hi()
print('Hello!')
Person Class instance
- name = Mario
- say_hi()
print('Hello!')
Person Class instance
- name = Lucia
- say_hi()
print('Hello!')
Object Oriented Programming
→ Logical Example
11. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Person Class
- name
- say_hi()
print('Hello!')
Person Class instance
- name = Mario
- say_hi()
print('Hello!')
Person Class instance
- name = Lucia
- say_hi()
print('Hello!')
Attribute (variabile)
Funcion (method)
Object Oriented Programming
→ Logical Example
12. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Object Oriented Programming
→ Class / instance attributes and methods
Person Class
- name
- say_hi()
print('Hello!')
By default, attributes and methods depend on the instance of the the class: they
behave differently for each instance.
However, if they don’t have to, then they can be
defined as class or static.
For example, the say_hi() function can be be
defined as a class method, as it produce the same
result regardless of the instance. If instead we
wanted to make the say_hi() function to include
the name of the person, then we couldn’t.
13. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
We use object for mainly two reasons:
- The allow to represent vey well hierarchies (and to exploit common
characteristics between them)
- Once instantiated, the allow to easily hold the status (without having to
rely on external support data structures)
Object Oriented Programming
→ Why to use objects
14. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
In Python there is a well defined styling convention:
- lowercase characters and underscores for variables and the object instances
- CamelCase for the class names
Moreover, double underscores before and after the name of a method mean that
that method is exclusively for internal (private) use, as for the string representation
(__str__) or the initiator of the object (__init__).
→ They are commonly called “magic methods”.
Object Oriented Programming
→ Conventions
15. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Object Oriented Programming
→ In Python everything is an object
16. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
examples.py examples.py
Object Oriented Programming
→ In Python everything is an object
17. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
examples.py examples.py
Operation (function, method) which
when executed returns a result
Operation (function, method) which when executed
changes the object, does not return anything!
Object Oriented Programming
→ Parenthesis: in-place operations
21. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
objects.py
“self” means “myself”, “myself class
instance”. It is mandatory in every
instance method, even if not used.
Object Oriented Programming
→ Defining objects The “init” function is responsible for
initializing the object. If it is not defined, the
default one is used, which does nothing.
22. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Object Oriented Programming
→ Defining objects
- To define class methods, use the @classmethod decorator. They have
the “cls” as first argument instead of the “self”
- To define static methods, use the @staticmethod decorator. They do
not have any special argument (no “self” nor “cls”).
→ A decorator is something placed above a function which “wraps”
the function and tells it to behave in a particular way
- To define static/class attributes, define them in the body of the class
23. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
objects.py
Object Oriented Programming
→ Defining objects
The “init” function is a magic method.
25. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
objects.py
The __str__ funcion is a magic
method as well, and it is responsible
for the string representation of the
object (i.e. when you print it)
Object Oriented Programming
→ Magic methods
28. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
End of part I
→ Questions?
Next: exercise 1
29. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Exercise 1
We want to write a predictive model for monthly shampoo sales.
30. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Exercise 1
We want to write a predictive model for monthly shampoo sales.
Our model is extremely simple:
- given a window of n
- the sales at t+1 are given by:
- the average increment computed over the previous n months
- summed to the last point (t) of the window
31. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Let’s chose to use 3 months for the prediction (n=3) and say that we want to
predict the sales for December (t+1).
We know that sales for September (t-2), October (t-1) and November (t) have
been, respectively, of 50, e 52 e 60 units.
Month Step Sales
September t-2 50
October t-1 52
November t (now) 60
December t+1 ?
Exercise 1
→ Example
32. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Let’s chose to use 3 months for the prediction (n=3) and say that we want to
predict the sales for December (t+1).
We know that sales for September (t-2), October (t-1) and November (t) have
been, respectively, of 50, e 52 e 60 units.
Month Step Sales
September t-2 50
October t-1 52
November t (now) 60
December t+1 (2+8)/2 + 60 = 65
Exercise 1
→ Example
33. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Exercise 1
The IncrementModel() class must have a fit() method (which does nothing)
and a predict() method. Both methods must take a “data” argument.
class IncrementModel():
def __init__(self, window)
self.window = window
def fit(self, data):
# Not implemented
pass
def predict(self, data):
# Compute and return the prediction
prediction = ...
return prediction
excercise.py