# Parent class
class Person:
# Constructor: a special type of subroutine called to create an object
def __init__(self, name, age, citizenship):
# class attributes
self.name = name
self.age = age
self.citizenship = {citizenship}
# Methods of the class
def update_age(self, new_age):
self.age = new_age
def add_citizenship(self, new_citizenship):
self.citizenship.add(new_citizenship)
def remove_citizenship(self, citizenship_to_remove):
if citizenship_to_remove in self.citizenship:
self.citizenship.remove(citizenship_to_remove)
def change_name(self, new_name):
self.name = new_name
def get_info(self):
print("Name of the person: {}".format(self.name))
print("Age of the person: {}".format(self.age))
print("Citizenships of the person: {}".format(self.citizenship))
###################################################################################################
# Children classes
class Student(Person):
def __init__(self, name, age, citizenship, major, school):
# super() is a special object created from the parent class
super().__init__(name, age, citizenship)
self.major = major
self.school = school
def change_major(self, new_major):
self.major = new_major
def change_school(self, new_school):
self.school = new_school
def get_info(self):
super().get_info()
print("Major: ", self.major)
print("School: ", self.school)
class Worker(Person):
def __init__(self, name, age, citizenship, position, company):
# super() is a special object created from the parent class
super().__init__(name, age, citizenship)
self.position = position
self.company = company
def change_position(self, new_position):
self.position = new_position
def change_company(self, new_company):
self.company = new_company
def get_info(self):
super().get_info()
print("Position: ", self.position)
print("Company: ", self.company)
# object kevin of the class Student
print("State 0")
kevin = Student("Kevin", 27, "indonesian", "Physics", "National University of Singapore")
kevin.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 1")
kevin.update_age(27)
kevin.add_citizenship("chinese")
kevin.get_info()
print("-----------------------------------------------------------------")
# modify some info:
print("State 2")
kevin.change_major("Computer Science")
kevin.school = "Heidelberg University" # it changes the attribute value directly
kevin.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 3")
kevin.add_citizenship("german")
kevin.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 4")
kevin.remove_citizenship("german")
kevin.get_info()
State 0
Name of the person: Kevin
Age of the person: 27
Citizenships of the person: {'indonesian'}
Major: Physics
School: National University of Singapore
-----------------------------------------------------------------
State 1
Name of the person: Kevin
Age of the person: 27
Citizenships of the person: {'chinese', 'indonesian'}
Major: Physics
School: National University of Singapore
-----------------------------------------------------------------
State 2
Name of the person: Kevin
Age of the person: 27
Citizenships of the person: {'chinese', 'indonesian'}
Major: Computer Science
School: Heidelberg University
-----------------------------------------------------------------
State 3
Name of the person: Kevin
Age of the person: 27
Citizenships of the person: {'chinese', 'indonesian', 'german'}
Major: Computer Science
School: Heidelberg University
-----------------------------------------------------------------
State 4
Name of the person: Kevin
Age of the person: 27
Citizenships of the person: {'chinese', 'indonesian'}
Major: Computer Science
School: Heidelberg University
# object antonio of the class Worker
print("State 0")
antonio = Worker("Antonio", 25, "mexican", "Software Developer", "Oracle")
antonio.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 1")
antonio.update_age(27)
antonio.add_citizenship("german")
antonio.get_info()
print("-----------------------------------------------------------------")
# modify some info:
print("State 2")
antonio.company = "Microsoft" # it changes the attribute value directly
antonio.change_position("Machine Learning Engineer")
antonio.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 3")
antonio.add_citizenship("french")
antonio.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 4")
antonio.remove_citizenship("french")
antonio.get_info()
State 0
Name of the person: Antonio
Age of the person: 25
Citizenships of the person: {'mexican'}
Position: Software Developer
Company: Oracle
-----------------------------------------------------------------
State 1
Name of the person: Antonio
Age of the person: 27
Citizenships of the person: {'mexican', 'german'}
Position: Software Developer
Company: Oracle
-----------------------------------------------------------------
State 2
Name of the person: Antonio
Age of the person: 27
Citizenships of the person: {'mexican', 'german'}
Position: Machine Learning Engineer
Company: Microsoft
-----------------------------------------------------------------
State 3
Name of the person: Antonio
Age of the person: 27
Citizenships of the person: {'mexican', 'french', 'german'}
Position: Machine Learning Engineer
Company: Microsoft
-----------------------------------------------------------------
State 4
Name of the person: Antonio
Age of the person: 27
Citizenships of the person: {'mexican', 'german'}
Position: Machine Learning Engineer
Company: Microsoft
class Person:
# Constructor: a special type of subroutine called to create an object
def __init__(self, name, age, citizenship):
# class attributes
self._name = name
self.__age = age
self.citizenship = {citizenship}
# Methods of the class
def update_age(self, new_age):
if isinstance(new_age, int):
self.__age = new_age
else:
raise TypeError("The paramter new_age must be an integer!")
def add_citizenship(self, new_citizenship):
self.citizenship.add(new_citizenship)
def remove_citizenship(self, citizenship_to_remove):
if citizenship_to_remove in self.citizenship:
self.citizenship.remove(citizenship_to_remove)
def change_name(self, new_name):
if isinstance(new_name, str):
self._name = new_name
else:
raise TypeError("The paramter new_name must be a string!")
def get_info(self):
print("Name of the person: {}".format(self._name))
print("Age of the person: {}".format(self.__age))
print("Citizenships of the person: {}".format(self.citizenship))
# object antonio of the class Person
print("State 0")
antonio = Person("Antonio", 25, "mexican")
antonio.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 1")
# the direct assigment does not throw an error but it does not change anything, since the attribute __age is private
antonio.__age = 27
antonio.add_citizenship("german")
antonio.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 2")
#antonio.update_age("27") # this throws an error
antonio.update_age(27)
antonio.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 3")
# the direct assigment does not throw an error but it does not change anything, since the attribute _name is private
antonio._name = "Juan"
#antonio.change_name(12345) # this throws an error
antonio.change_name("Juan")
antonio.get_info()
State 0
Name of the person: Antonio
Age of the person: 25
Citizenships of the person: {'mexican'}
-----------------------------------------------------------------
State 1
Name of the person: Antonio
Age of the person: 25
Citizenships of the person: {'mexican', 'german'}
-----------------------------------------------------------------
State 2
Name of the person: Antonio
Age of the person: 27
Citizenships of the person: {'mexican', 'german'}
-----------------------------------------------------------------
State 3
Name of the person: Juan
Age of the person: 27
Citizenships of the person: {'mexican', 'german'}
import numpy as np
# Parent class
class GeometricFigure:
def __init__(self, name, number_edges):
self.name = name
self.number_edges = number_edges
def get_info(self):
print("Name of the figure: {}".format(self.name))
print("Number of edges: {}".format(self.number_edges))
###################################################################################################
# Children classes
class Square(GeometricFigure):
def __init__(self, name, number_edges, length_side):
super().__init__(name, number_edges)
self.length_side = length_side
self.__area = 0
self.__perimeter = 0
def compute_area(self):
self.__area = self.length_side ** 2
def compute_perimeter(self):
self.__perimeter = 4 * self.length_side
def get_info(self):
super().get_info()
if self.__area == 0:
print("Area not computed yet!")
else:
print("Area: {}".format(self.__area))
if self.__perimeter == 0:
print("Perimeter not computed yet!")
else:
print("Perimeter: {}".format(self.__perimeter))
class EquilateralTriangle(GeometricFigure):
def __init__(self, name, number_edges, length_side):
super().__init__(name, number_edges)
self.length_side = length_side
self.__area = 0
self.__perimeter = 0
def compute_area(self):
self.__area = (self.length_side ** 2) * np.sin(np.pi/3)/2
def compute_perimeter(self):
self.__perimeter = 3 * self.length_side
def get_info(self):
super().get_info()
if self.__area == 0:
print("Area not computed yet!")
else:
print("Area: {}".format(self.__area))
if self.__perimeter == 0:
print("Perimeter not computed yet!")
else:
print("Perimeter: {}".format(self.__perimeter))
# object square_1 from class Square
print("State 0")
square_1 = Square("Square", 4, 5)
square_1.get_info()
print("-----------------------------------------------------------------")
# compute area and perimeter
print("State 1")
square_1.compute_area()
square_1.compute_perimeter()
square_1.get_info()
print("##################################################################")
# object triangle_1 from class EquilateralTriangle
print("State 0")
triangle_1 = EquilateralTriangle("Equilateral Triangle", 3, 2)
triangle_1.get_info()
print("-----------------------------------------------------------------")
# compute area and perimeter
print("State 1")
triangle_1.compute_area()
triangle_1.compute_perimeter()
triangle_1.get_info()
State 0
Name of the figure: Square
Number of edges: 4
Area not computed yet!
Perimeter not computed yet!
-----------------------------------------------------------------
State 1
Name of the figure: Square
Number of edges: 4
Area: 25
Perimeter: 20
##################################################################
State 0
Name of the figure: Equilateral Triangle
Number of edges: 3
Area not computed yet!
Perimeter not computed yet!
-----------------------------------------------------------------
State 1
Name of the figure: Equilateral Triangle
Number of edges: 3
Area: 1.7320508075688772
Perimeter: 6
# Now, we use polymorphism to compute area and perimeter of the figures
square_1 = Square("Square", 4, 5)
square_2 = Square("Square", 4, 10)
triangle_1 = EquilateralTriangle("Equilateral Triangle", 3, 2)
triangle_2 = EquilateralTriangle("Equilateral Triangle", 3, 4)
list_figures = [square_1, square_2, triangle_1, triangle_2]
for figure in list_figures:
# polymorphism applied
figure.compute_area()
figure.compute_perimeter()
figure.get_info()
print("##################################################################")
Name of the figure: Square
Number of edges: 4
Area: 25
Perimeter: 20
##################################################################
Name of the figure: Square
Number of edges: 4
Area: 100
Perimeter: 40
##################################################################
Name of the figure: Equilateral Triangle
Number of edges: 3
Area: 1.7320508075688772
Perimeter: 6
##################################################################
Name of the figure: Equilateral Triangle
Number of edges: 3
Area: 6.928203230275509
Perimeter: 12
##################################################################