SlideShare a Scribd company logo
Python Advanced Course
Part I
Stefano Alberto Russo
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
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
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?
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Living organisms
Reptiles
Mammals
Bipeds Quadrupeds
Object Oriented Programming
→ What is it?
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?
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Glasses
Glass Plastic
Coloured Transparent
Painted
Compound
Recyclable
Not
Recyclable
Object Oriented Programming
→ What is it?
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?
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
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
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
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.
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
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
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Object Oriented Programming
→ In Python everything is an object
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
examples.py examples.py
Object Oriented Programming
→ In Python everything is an object
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
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
objects.py
Object Oriented Programming
→ Defining objects
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
objects.py
instantiation
Object Oriented Programming
→ Defining objects
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
objects.py
Object Oriented Programming
→ Defining objects
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.
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
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
objects.py
Object Oriented Programming
→ Defining objects
The “init” function is a magic method.
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
objects.py
Object Oriented Programming
→ Magic methods
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
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
objects.py
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
objects.py
Instance method (function)
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
End of part I
→ Questions?
Next: exercise 1
Stefano Alberto Russo - @stefanoarusso - sarusso.github.io
Exercise 1
We want to write a predictive model for monthly shampoo sales.
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
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
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
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

More Related Content

PPTX
Python-Classes.pptx
Karudaiyar Ganapathy
 
PPTX
Object Oriented Programming.pptx
SAICHARANREDDYN
 
PPTX
software construction and development week 3 Python lists, tuples, dictionari...
MuhammadBilalAjmal2
 
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
Harsha Patil
 
PPTX
object oriented porgramming using Java programming
afsheenfaiq2
 
PPTX
Regex,functions, inheritance,class, attribute,overloding
sangumanikesh
 
PPTX
مقدمة بايثون .pptx
AlmutasemBillahAlwas
 
PPTX
Object oriented Programming in Python.pptx
SHAIKIRFAN715544
 
Python-Classes.pptx
Karudaiyar Ganapathy
 
Object Oriented Programming.pptx
SAICHARANREDDYN
 
software construction and development week 3 Python lists, tuples, dictionari...
MuhammadBilalAjmal2
 
Unit 3-Classes ,Objects and Inheritance.pdf
Harsha Patil
 
object oriented porgramming using Java programming
afsheenfaiq2
 
Regex,functions, inheritance,class, attribute,overloding
sangumanikesh
 
مقدمة بايثون .pptx
AlmutasemBillahAlwas
 
Object oriented Programming in Python.pptx
SHAIKIRFAN715544
 

Similar to Python Advanced Course - part I.pdf for Python (20)

PPTX
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
 
PPT
07slide.ppt
NuurAxmed2
 
PPTX
UNIT-5 object oriented programming lecture
w6vsy4fmpy
 
PDF
Lecture 01 - Basic Concept About OOP With Python
National College of Business Administration & Economics ( NCBA&E)
 
PDF
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
PPSX
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
PPTX
Presentation_4516_Content_Document_20250204010703PM.pptx
MuhammadChala
 
PDF
Object Oriented Programming by Using Python
trainermadhusudhan
 
PPTX
Object-Oriented Programming in Python.pptx
ssuser4ab3a2
 
PPTX
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
PPTX
OOPS.pptx
NitinSharma134320
 
PPTX
Object Oriented Programming Class and Objects
rubini8582
 
PPTX
VTU Python Module 5 , Class and Objects and Debugging
rickyghoshiit
 
PPTX
classes and objects of python object oriented
VineelaThonduri
 
PPT
Chap 3 Python Object Oriented Programming - Copy.ppt
muneshwarbisen1
 
PPTX
OOP Concepts Python with code refrences.pptx
SofiMusic
 
PPTX
oogshsvshsbhshhshvsvshsvsvhshshjshshhsvgps.pptx
BhojarajTheking
 
PPTX
oopinpyhtonnew-140722060241-phpapp01.pptx
smartashammari
 
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
 
07slide.ppt
NuurAxmed2
 
UNIT-5 object oriented programming lecture
w6vsy4fmpy
 
Lecture 01 - Basic Concept About OOP With Python
National College of Business Administration & Economics ( NCBA&E)
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
Presentation_4516_Content_Document_20250204010703PM.pptx
MuhammadChala
 
Object Oriented Programming by Using Python
trainermadhusudhan
 
Object-Oriented Programming in Python.pptx
ssuser4ab3a2
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
Object Oriented Programming Class and Objects
rubini8582
 
VTU Python Module 5 , Class and Objects and Debugging
rickyghoshiit
 
classes and objects of python object oriented
VineelaThonduri
 
Chap 3 Python Object Oriented Programming - Copy.ppt
muneshwarbisen1
 
OOP Concepts Python with code refrences.pptx
SofiMusic
 
oogshsvshsbhshhshvsvshsvsvhshshjshshhsvgps.pptx
BhojarajTheking
 
oopinpyhtonnew-140722060241-phpapp01.pptx
smartashammari
 
Ad

More from JunZhao68 (20)

PDF
语法专题3-状语从句.pdf 英语语法基础部分,涉及到状语从句部分的内容来米爱上
JunZhao68
 
PDF
愛小孩的歐拉一 兼論 108 數學課綱.pdf for 欧拉&数论相关课程描述啊
JunZhao68
 
PDF
svd15_86.pdf for SVD study and revosited
JunZhao68
 
PDF
Quadra-T1-T2-T4_TechSpec.pdf for netint VPA
JunZhao68
 
PDF
Python Advanced Course - part III.pdf for Python
JunZhao68
 
PDF
3 - Intro to SVE.pdf for intro ARM SVE part
JunZhao68
 
PDF
pytorch-cheatsheet.pdf for ML study with pythroch
JunZhao68
 
PDF
Vocabulary Cards for AI and KIDs MIT.pdf
JunZhao68
 
PDF
how CNN works for tech Every parts introductions.pdf
JunZhao68
 
PDF
eics22-slides for researchers need when implementing novel imteraction tech
JunZhao68
 
PDF
Netflix-talk for live video streaming tech
JunZhao68
 
PPTX
Linear system 1_linear in linear algebra.pptx
JunZhao68
 
PDF
GDC2012 JMV Rotations with jim van verth
JunZhao68
 
PDF
1-MIV-tutorial-part-1.pdf
JunZhao68
 
PDF
GOP-Size_report_11_16.pdf
JunZhao68
 
PDF
02-VariableLengthCodes_pres.pdf
JunZhao68
 
PDF
MHV-Presentation-Forman (1).pdf
JunZhao68
 
PDF
CODA_presentation.pdf
JunZhao68
 
PDF
http3-quic-streaming-2020-200121234036.pdf
JunZhao68
 
PDF
NTTW4-FFmpeg.pdf
JunZhao68
 
语法专题3-状语从句.pdf 英语语法基础部分,涉及到状语从句部分的内容来米爱上
JunZhao68
 
愛小孩的歐拉一 兼論 108 數學課綱.pdf for 欧拉&数论相关课程描述啊
JunZhao68
 
svd15_86.pdf for SVD study and revosited
JunZhao68
 
Quadra-T1-T2-T4_TechSpec.pdf for netint VPA
JunZhao68
 
Python Advanced Course - part III.pdf for Python
JunZhao68
 
3 - Intro to SVE.pdf for intro ARM SVE part
JunZhao68
 
pytorch-cheatsheet.pdf for ML study with pythroch
JunZhao68
 
Vocabulary Cards for AI and KIDs MIT.pdf
JunZhao68
 
how CNN works for tech Every parts introductions.pdf
JunZhao68
 
eics22-slides for researchers need when implementing novel imteraction tech
JunZhao68
 
Netflix-talk for live video streaming tech
JunZhao68
 
Linear system 1_linear in linear algebra.pptx
JunZhao68
 
GDC2012 JMV Rotations with jim van verth
JunZhao68
 
1-MIV-tutorial-part-1.pdf
JunZhao68
 
GOP-Size_report_11_16.pdf
JunZhao68
 
02-VariableLengthCodes_pres.pdf
JunZhao68
 
MHV-Presentation-Forman (1).pdf
JunZhao68
 
CODA_presentation.pdf
JunZhao68
 
http3-quic-streaming-2020-200121234036.pdf
JunZhao68
 
NTTW4-FFmpeg.pdf
JunZhao68
 
Ad

Recently uploaded (20)

PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Landforms and landscapes data surprise preview
jpinnuck
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 

Python Advanced Course - part I.pdf for Python

  • 1. Python Advanced Course Part I Stefano Alberto Russo
  • 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
  • 18. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io objects.py Object Oriented Programming → Defining objects
  • 19. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io objects.py instantiation Object Oriented Programming → Defining objects
  • 20. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io objects.py Object Oriented Programming → Defining objects
  • 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.
  • 24. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io objects.py Object Oriented Programming → Magic methods
  • 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
  • 26. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io objects.py
  • 27. Stefano Alberto Russo - @stefanoarusso - sarusso.github.io objects.py Instance method (function)
  • 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