Classes and Objects | Object Oriented Programming in Python - Part 1

Introduction:

Python is one of the most versatile languages of the current day. Programmers, Data Scientists, Statisticians, and a variety of other people prefer Python. Programmers often tend to use programming methods like Object Oriented Programming for writing huge programs, due to its multiple advantages over general programming. In this post, let's have a quick look at how we can implement OOP concepts in Python. 

Not everyone is an expert in OOP, so why don't we start with a quick introduction?

What is Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a way of organizing your code by grouping related data and behaviors into "objects." An object represents a real-world entity, like a car or a person, with its own attributes (data) and actions (methods). Instead of writing scattered functions and variables, OOP helps you keep them together in a structured format, making your code easier to manage, reuse, and extend.

At the core of OOP are four key principles:

  1. Encapsulation: Bundling data and methods that operate on that data inside a class and hiding details from the outside world.
  2. Inheritance: Creating new classes based on existing ones, allowing code reuse and the building of more specific features.
  3. Polymorphism: Allowing objects to be treated as instances of their parent class, while still maintaining their own unique behavior.
  4. Abstraction: Simplifying complex systems by focusing only on the essential details, hiding the unnecessary ones.

These four principles make OOP a highly preferred programming paradigm. Let us get into each one in detail.

At its very base, OOP is built on two things - classes and objects. Classes are blueprints, on which objects can be built. Each object is an instance or one item created based on any class. Any number of objects can be built from a class. 

Inside each class, we can define variables (sometimes called attributes) and functions (sometimes called methods). Let's understand this better with an example:

# Defining a class
class Dog:
    # The __init__ method initializes the object's attributes
    def __init__(self, name, age):
        self.name = name  # Attribute: Name of the dog
        self.age = age    # Attribute: Age of the dog
    
    # Method: This defines a behavior for the object
    def bark(self):
        print(f"{self.name} says Woof!")

# Creating objects (instances of the Dog class)
dog1 = Dog("Buddy", 3)  # Creating a dog named Buddy, age 3
dog2 = Dog("Charlie", 5)  # Creating another dog named Charlie, age 5

# Accessing attributes and methods
print(f"Dog 1: {dog1.name}, Age: {dog1.age}")  # Output: Dog 1: Buddy, Age: 3
dog1.bark()  # Output: Buddy says Woof!

print(f"Dog 2: {dog2.name}, Age: {dog2.age}")  # Output: Dog 2: Charlie, Age: 5
dog2.bark()  # Output: Charlie says Woof!

What does this code even mean, you ask? Let's break it down. 

class Dog - we define a class named Dog, which contains a def __init__(self, attr) statement. This def __init__ method is very important and contains all the data that should be created for an object as soon as it's created.  

self.name and self.age: We create two attributes (variables) called name and age. We say self.attr to tell Python that these attributes must be considered different for each object. 

def bark: We create a function to print a statement. 

Then, we create two objects using the Dog class, called dog1 and dog2. We give dog1 the name Buddy, and age 3. We give dog2 the name Charlie, and an age of 5. 

Now, when we call dog1.bark(), we see that the function bark, prints self.name and "says Woof". So, since dog1.name has been set as Buddy, we get Buddy says Woof! I think now you should be able to say pretty easily what happens when I call dog2.bark()

Now, that you have some familiarity with Classes and Objects, we can move to more advanced concepts of OOP. 

Keep an eye out for the next post on OOP in Python. Until then, do check out some of our other posts. If you like the posts or have a question, please do let me know in the comments and I'll get back as soon as I can. 

Comments