Object-oriented programming
Gianfranco Lombardo, Ph.D
gianfranco.lombardo@unipr.it
Object Oriented Programming
● Object Oriented Programming (OOP) is a programming paradigm based on
the concept of "objects"
● An object contains data in the form of fields (called attributes)
● At the same time it is something more complex than just a variable because it
offers services by providing functions to manage data inside (methods)
● They have an internal state
● For now, we can think to objects as the initialization of a custom data type that
someone have defined previously
● The big novel: Everything in Python is an object and you already used
objects in our previous exercises !
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
OOP: What is an object?
● Represents a concept or a way to model physical objects (e.g, a sensor, a
product, a character in a video game! etc..)
● Example:
○ object name: “Mario’s car”
○ class : car #We will define it soon
○ attributes:
■ 4 wheels
■ Speed
■ Current gear
○ methods:
■ acceleration()
■ drive()
■ change gear()
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
OOP: What is a class ?
● A class defines the structure of an object:
○ What are the attributes type and name
○ What are the methods that the object has to offer
○ What is the code that methods should execute
● Each object has a class that define its characteristics
● An object is the concrete form of a class!
● Example: class of soccer teams, each soccer teams has 11 players etc..
○ Objects of type “soccer teams”: Juventus, Milan, Inter, Parma...so on
● Example: class car
○ Objects: mario’s car, alice’s car, george’s car
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
OOP: Class Vs Objects
● Class ● Object
○ What is the concept? ○ Concrete instance that we can
○ How does it work? use
○ How is it build? ○ It is built depending on the
rules that its class defines
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
Everything is an object
● You already used objects in Python!
● Everything is implemented as an object!
● When you define:
○ value=5 an object called “value” is created from class “Integer” that
defines what is an int value and what methods we can use on it to
change its internal state
○ products_list=[] an object from class list is created
○ message = “Hello world” an object from class String is created
● In fact, on these objects for example strings we called some functions to
modify their internal state
○ .split()
○ .replace()
○ .append() for lists
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
OOP: Class in Python
● Each class should define:
○ The internal state of an object with attributes (read variable under control
of each object)
○ Methods to modify the internal state (read functions)
○ A special function called “constructor” that permits to build an object of
that class
■ It takes arguments (optionally) to define the initial internal state and
return an instance of an object of that class type
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
Define a class
class Product: Py
def __init__(self, name,price,number):
self._name = name # attribute self: special keyword that
self._price = price # attribute say to interpreter that
self._number = number # attribute methods or attributes value
have to be assigned to each
def get_name(self): #method object that is built from this
return self._name class !
def get_price(self): #method
return self._price
def set_price(self,new_price): #method
self._price=new_price
def main():
p = Product("pen",1.50,200) #Instantiation of an object p (Product type)
print(p.get_name()) #call of a method on object P
print(p.get_price())
p.set_price(2.00)
print(p.get_price())
main()
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
Special objects with special classes
● Now more things about Python should be more clear (or at least I hope :D)
● Everything is an object so everything refers to particular classes!
● For example: integer values are instances (object) of class Integer
○ For this primitive class (built-in in Python) special constructors functions
are defined
○ We say n=5 but in theory we can also say n = int(5)
■ The way we were doing variable casting!
● So remember when you use “built-in” data type you can call the available
methods on these objects!
● You can also define your custom classes and instantiate your own objects!
● You can combine these things: For example a list of your custom objects!
● You can use classes defined by somebody else!
○ Software library or API
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
Define a class
class Product: Py
def __init__(self, name,price,number):
self._name = name # attribute
self._price = price # attribute
self._number = number # attribute
def get_name(self): #method
return self._name
def get_price(self): #method
return self._price
def set_price(self,new_price): #method
self._price=new_price
def main():
p = Product("pen",1.50,200) #Instantiation of an object p (Product type)
print(p.get_name()) #call of a method on object P
print(p.get_price())
p.set_price(2.00)
print(p.get_price())
main()
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
List of objects
Py
n=0
products_list=[]
while n<10:
request = input(“Insert product,price,quantity”)
fields= request.split(“,”)
p = Product(fields[0],float(fields[1]),float(fields[2]))
products_list.append(p)
n+=1
for item in products_list:
p.set_price(p.get_price()+2)
p.buy() # this method could reduce the number of available items ! Has to be defined in the class
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
Modify product class
class Product: Py
……..
def get_name(self): #method
return self._name
def get_price(self): #method
return self._price
def set_price(self,new_price): #method
self._price=new_price
def buy(self):
if self._number >0 :
self._number-=1
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
BREAK
Gianfranco Lombardo, Ph.D (gianfranco.lombardo@unipr.it)
Exercise: Registry with OOP and files
● Starting from previous Registry exercise
○ Define a class Person with attributes name, last name and age
○ Define methods you will need to set these attributes and to compute the
age
○ Define a list of objects Person and put each item in this list
○ After 10 items, iterate over the list and write all information on a file
“registry.txt”
■ One person for each row
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)
Exercise: Registry with OOP and files
● Read registry.txt, create an object for each person and add to a list
● Compute the average age in the registry by defining an apposite function
● Try to order your list depending on the age
○ Advice: Use a second list and add items directly ordered by iterating over
the first list
● After that, write on a file again people ordered using “append” mode on the
same registry.txt file
2020 - Gianfranco Lombardo, MEng (gianfranco.lombardo@unipr.it)