Model and Object-Oriented Programming
Object Oriented Programming is a part of learning Python. The objective of this blog is to introduce OOP with the intention of PBL task to create a database. The foundations for a database is defining a Class and understanding instance data and methods. A database is often a focus of backend coding as it will store persistent data, that can be recalled after the immediate session is closed.
Class and Object Terms
The foundations of Object-Oriented Programming is defining a Class
- In Object-Oriented Programming (OOP), a class is a blueprint for creating an Object. (a data structure). An Object is used like many other Python variables.
- A Class has ...
- a collection of data, these are called Attributes and in Python are pre-fixed using the keyword self
- a collection of Functions/Procedures. These are called *Methods when they exist inside a Class definition.
- An Object is created from the Class/Template. Characteristics of objects ...
- an Object is an Instance of the Class/Template
- there can be many Objects created from the same Class
- each Object contains its own Instance Data
- the data is setup by the Constructor, this is the "init" method in a Python class
- all methods in the Class/Template become part of the Object, methods are accessed using dot notation (object.method())
- A Python Class allow for the definition of @ decorators, these allow access to instance data without the use of functions ...
- @property decorator (aka getter). This enables developers to reference/get instance data in a shorthand fashion (object.name versus object.get_name())
- @name.setter decorator (aka setter). This enables developers to update/set instance data in a shorthand fashion (object.name = "John" versus object.set_name("John"))
- observe all instance data (self._name, self.email ...) are prefixed with "", this convention allows setters and getters to work with more natural variable name (name, email ...)
from datetime import date
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
dob = date(2004, 12, 31)
age = calculate_age(dob)
print(age)
class Student:
def __init__(self, name, major, gpa, classOf):
self.name = name
self.major = major
self.gpa = gpa
self.classOf = classOf
self.dob = None
self.age = None
def set_dob(self, dob):
self.dob = dob
self.age = self.calc_age()
def calc_age(self):
today = date.today()
age = today.year - self.dob.year - ((today.month, today.day) < (self.dob.month, self.dob.day))
return age
def get_classOf(self):
return self.classOf
def get_age(self):
return self.age
def tester():
student1 = Student("Jim", "Business", 3.1, 2019)
student1.set_dob(date(1994, 2, 21))
print("Student's name is: " + student1.name)
print("Student's class of: " + str(student1.get_classOf()))
print("Student's age is: " + str(student1.get_age()) + " years old")
tester()
class FastFood:
def __init__(self, name, price, ingredients):
self.name = name
self.price = price
self.ingredients = ingredients
# getters
def get_name(self):
return self.name
def get_price(self):
return self.price
def get_ingredients(self):
return self.ingredients
# setters
def set_name(self, name):
self.name = name
def set_price(self, price):
self.price = price
def set_ingredients(self, ingredients):
self.ingredients = ingredients
# tester
burger = FastFood("Hamburger", 5.99, ["Bun", "Beef Patty", "Lettuce", "Tomato", "Onion"])
print(burger.get_name())
print(burger.get_price())
print(burger.get_ingredients())
burger.set_price(6.99)
print(burger.get_price())