13/01/2024 • 2 Minute Read

Object Oriented Programming in Python

Mastering the Fundamentals: An In-Depth Guide to Object-Oriented Programming in Python

placeholder image

Software Design

Python

AI Generated

Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs.

Classes in Python

In Python, everything is an object. You can use the class keyword to define new types of objects.

Properties

The idiomatic way to add fields or properties to a class is using the __init__ method which is the constructor. It can take parameters like a regular function (including optional parameters). To reference the object itself once it is instantiated, we use self.

Example:

class Person:
	def __init__(self, fName, sName, age=0):
		self.fName = fName
		self.sName = sName
		self.age = age

Methods

Class methods are written very similarly to functions, except they can take self which refers to the instantiated object.

Example:

class Person:
	def __init__(self, fName, sName, age=0):
		self.fName = fName
		self.sName = sName
		self.age = age

	def getFirstName(self):
		return self.fName

	def getSurName(self):
		return self.sName

	def incrementAge(self):
		self.age = self.age + 1

Magic or Dunder Methods

Magic methods (also referred to as dunder methods) are special methods which enable certain functionality. They start and end with two underscores. In the case of __str__ it runs when you cast the object as a str. In the case of __eq__ it runs when you compare two objects to check if they are the same.

Example:

import uuid

class Person:
	def __init__(self, fName, sName):
		self.id = uuid.uuid4()
		self.fName = fName
		self.sName = sName

	def __str__(self):
		return f"{self.fName} {self.sName}"

	def __eq__(self, otherPerson):
		return self.id == otherPerson.id

Reusability Concepts in Python

Inheritance

Inheritance is a principle in OOP that allows a class to inherit methods and properties from another class. The class that is being inherited from is called the parent class, and the class that inherits is called the child class.

Example:

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)
    self.graduationyear = 2019

Composition

Composition is an alternative to inheritance and is a way to combine simple objects or data types into more complex ones. Composition represents a part-of relationship.

Example:

class Salary:
    def __init__(self, monthly_income):
        self.monthly_income = monthly_income

    def get_total(self):
        return (self.monthly_income*12)

class Employee:
    def __init__(self, monthly_income, bonus):
        self.monthly_income = monthly_income
        self.bonus = bonus
        self.obj_salary = Salary(self.monthly_income)

    def annual_salary(self):
        return "Total: " + str(self.obj_salary.get_total() + self.bonus) + ' €'

obj_emp = Employee(2600, 500)
print(obj_emp.annual_salary())