DEV Community

Odipo Otieno (KwargDevs)
Odipo Otieno (KwargDevs)

Posted on

Introduction to Object-Oriented Programming (OOP) in Python


To Support My YouTube Channel: Click Here

I need like 630 subs to reach 1000 and get monetized

Thanks in Advance 🙏


Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design software. These objects can be anything you want to model in your program, such as a person, car, or bank account. Each object can have properties (attributes) and actions (methods) it can perform.

Let's break down the basic concepts of OOP:

  1. Class: A blueprint for creating objects. It defines a set of attributes and methods that the created objects (instances) will have.
  2. Object: An instance of a class. When a class is defined, no memory is allocated until an object of that class is created.
  3. Attribute: A variable that belongs to an object or class. Attributes are used to store information about the object.
  4. Method: A function that belongs to an object or class. Methods define the behaviors of an object.

Example

Let's create a simple class to understand these concepts better.

Step 1: Define a Class

class Dog:
    # This is a class attribute
    species = "Canis familiaris"

    # The __init__ method initializes the object's attributes
    def __init__(self, name, age):
        self.name = name  # instance attribute
        self.age = age    # instance attribute

    # An instance method
    def bark(self):
        return f"{self.name} says woof!"
Enter fullscreen mode Exit fullscreen mode

Step 2: Create an Object (Instance of the Class)

my_dog = Dog("Buddy", 5)
Enter fullscreen mode Exit fullscreen mode

Here, my_dog is an instance of the Dog class. It has attributes name and age, and it can use the method bark.

Step 3: Access Attributes and Methods

# Accessing attributes
print(my_dog.name)  # Output: Buddy
print(my_dog.age)   # Output: 5
print(my_dog.species)  # Output: Canis familiaris

# Calling a method
print(my_dog.bark())  # Output: Buddy says woof!
Enter fullscreen mode Exit fullscreen mode

Breaking Down the Code

  1. Class Definition (class Dog):

    • class Dog: defines a new class named Dog.
    • species is a class attribute shared by all instances of the Dog class.
  2. The __init__ Method:

    • __init__ is a special method called a constructor. It is automatically called when a new instance of the class is created.
    • self refers to the instance of the class. It is used to access instance attributes and methods.
    • self.name and self.age are instance attributes unique to each instance.
  3. Instance Method (def bark):

    • bark is a method that belongs to the Dog class. It uses the self parameter to access the instance's attributes.

More About Classes and Objects

  1. Class Attributes vs. Instance Attributes:

    • Class attributes are shared across all instances of the class.
    • Instance attributes are unique to each instance.
  2. Encapsulation:

    • Encapsulation is the concept of bundling data (attributes) and methods that operate on the data within one unit (class).
  3. Inheritance:

    • Inheritance is a way to form new classes using classes that have already been defined. It helps in reusability.
  4. Polymorphism:

    • Polymorphism allows methods to do different things based on the object it is acting upon, even if they share the same name.

Example of Inheritance

class Puppy(Dog):
    def __init__(self, name, age, color):
        super().__init__(name, age)  # Initialize attributes from the parent class
        self.color = color  # New attribute unique to Puppy class

    def bark(self):
        return f"{self.name} says yap!"

my_puppy = Puppy("Bella", 1, "brown")
print(my_puppy.bark())  # Output: Bella says yap!
print(my_puppy.color)   # Output: brown
Enter fullscreen mode Exit fullscreen mode

In this example, Puppy is a subclass of Dog. It inherits attributes and methods from Dog but can also have its own additional attributes and methods.

Top comments (0)