Object Oriented Programming(OOP) in Python

Object Oriented Programming(OOP) in Python

Introduction

Imagine you're a chef in a busy kitchen. You have various ingredients, utensils, and recipes. To run a smooth operation, you need an organized kitchen layout and a system to manage everything efficiently.

Object-Oriented Programming (OOP) is your recipe for a well-structured kitchen. It helps you model each ingredient, utensil, and recipe as objects. OOP ensures that everything has a defined role, is encapsulated neatly, and interacts seamlessly, just like a well-organized kitchen. It simplifies complex software development, making it easier to maintain and expand.

What is Object Oriented Programming?

Object-oriented programming, commonly referred to as OOP, is a fundamental programming framework used in software development. Fundamentally, OOP makes you model around the real world in your code. It treats software components like they were real-world objects, each of them has its characteristics (attributes) and behaviors (methods). These objects relate with one another to solve different tasks, just like how we relate with people in our daily lives. OOP provides an organized way to design and build software, making it easier to understand, easy to maintain, and it can match with how we understand and interact with the world around us.

OOP Principles

  • Classes and Objects

  • Attributes

  • Methods

  • Encapsulation

  • Abstraction

  • Inheritance

  • Polymorphism

Classes and Objects

In object-oriented programming (OOP), objects are the fundamental building blocks for modeling and representing real-world entities or concepts. Objects encapsulate data and behaviors related to these entities.

Classes

In Python*, a* class is a blueprint or template for creating objects*. It defines the attributes (data) and methods (functions) that the* objects created from that class will have. Classes are defined using the class keyword.

class Car:
    def __init__(self, maker, color):
        self.maker = maker
        self.color = color

    def car_introduce(self):
       print("It's a brand of {} and it's colour is {}".format(self.maker, self.color))

Objects

Objects are instances of classes*. They are created based on the* class definition and represent specific instances of the entity described by the class*.*

Objects include characteristics (also known as properties) and methods (related functions).

my_car = Car("Toyota", "Black")

Attributes

Attributes are variables that store information about an object*, and they represent an* object's qualities or characteristics.

Attributes in Python are declared within a class and accessed using dot notation.

class MyClass:
  name = "Samuel"
  age = 42

obj = MyClass()
"""Accessing attribute1"""
value1 = obj.name
"""Accessing attribute2"""
value2 = obj.age

Methods

  • Methods are functions specified within a class that perform operations on the properties of class objects.

  • They encapsulate object-related activity.

  • The'self' argument refers to the object instance invoking the method.

person1 = Person("Samuel", 30)
   """Calling the 'greet' method"""
   greeting = person1.greet()
   print(greeting)

Encapsulation

This State, variables, and methods should be private unless they are being declared public.

An object “car” of a class “Ride” can manage itself via methods “start” and other class “House” cannot touch it except if allowed to. We can only use the methods to communicate with our object. If we want another class “House” to change the state of the “car” object, we have to create public methods “over_ride_and_start” to modify or invoke the start variable. This connection is encapsulation.

Abstraction

Abstraction Hide all implementation of the class from anything outside the class.

Our object should hide details and only expose relevant information to another object. Thus public method should be created so the object can be called.

Inheritance

The child class should have a mechanism that enables it to inherit behavior and properties from the parent class.

Reusability is the advantage here. Child class “car”, and “boat” objects should acquire properties (“has_an_engine”, “can_be_used_for_transport”) from the parent class “Ride”. However, they should have their unique properties.

Polymorphism

Interfacing with objects and receiving different forms or results.

This allows us to implement a class exactly as intended for the parent class. This prevents errors in types and each sub-class keeps its methods. Our class “Ride” has a speed method. Our child class; “Object”, “boat” and “plane” can utilize this method. However, they would each have their iteration of the speed method.

Pitfalls in OOP

  • Lack of Data Encapsulation: Data is frequently global or shared among multiple procedures in a Procedure Oriented Approach. This might result in inadvertent data alteration, making data encapsulation and data integrity challenging.

  • Limited Reusability: Procedures are often built for distinct purposes, making code reuse difficult. This can lead to code duplication and lower maintainability.

  • Poor Scalability: As a program expands, managing numerous procedures can become overwhelming, making it challenging to scale the program to efficiently handle complex tasks.

  • Difficulty in Understanding: The Procedure Oriented Approach can be challenging to comprehend and debug for those who didn't initially write the code.

  • Lack of Abstraction: The Procedure Oriented Approach can be challenging for those who haven't written the code initially.

Summary

I hope you will find this post useful and learn something new in the process. If you have any questions or suggestions let me know in the comment section.

Feedback will help me a lot and motivate me to share more content like this in the future.

If you have read this far, Thank you very much!

Connect with me on Twitter | LinkedIn | Github

Happy Coding!