Object-Oriented Programming (OOP) in Python

 

What is OOP? 

Object-Oriented Programming (OOP) is a way to structure your code by bundling related properties and behaviors into objects. Instead of just writing functions and variables, you create classes that define the blueprint for objects. Imagine you're designing a game: instead of having separate variables for each character's name, health, and attack power, you create a Character class!

Creating a Class and Object

A class is a blueprint, and an object is an instance of that class.

class Dog:
    def __init__(self, name, breed):
        self.name = name  # Instance variable
        self.breed = breed
    
    def bark(self):
        return f"{self.name} says Woof!"

# Creating objects (instances)
dog1 = Dog("Buddy", "Golden Retriever")
dog2 = Dog("Max", "Bulldog")

print(dog1.bark())  # Buddy says Woof!
print(dog2.bark())  # Max says Woof!

The __init__ Method

The __init__ method is a constructor that initializes object attributes when an object is created.

class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

car1 = Car("Tesla", "Model S")
print(car1.brand)  # Tesla

Class vs Instance Variables

  • Instance variables: Unique for each object (self.name)
  • Class variables: Shared across all instances
class Animal:
    species = "Mammal"  # Class variable
    
    def __init__(self, name):
        self.name = name  # Instance variable

cat = Animal("Whiskers")
dog = Animal("Rex")

print(cat.species)  # Mammal (shared)
print(dog.name)  # Rex (unique)

Inheritance

Inheritance allows a class to inherit properties and methods from another class.

class Animal:
    def __init__(self, name):
        self.name = name
    
    def speak(self):
        return "I make a sound!"

class Dog(Animal):  # Inherits from Animal
    def speak(self):
        return "Woof!"

my_dog = Dog("Buddy")
print(my_dog.name)  # Buddy
print(my_dog.speak())  # Woof!

Encapsulation

Encapsulation restricts access to certain parts of an object to protect data integrity.

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance  # Private variable

    def deposit(self, amount):
        self.__balance += amount
        return self.__balance

account = BankAccount(1000)
print(account.deposit(500))  # 1500
#print(account.__balance)  #  AttributeError: Private variable!

Polymorphism

Polymorphism lets different classes have methods with the same name but different behavior.

class Cat:
    def speak(self):
        return "Meow!"

class Dog:
    def speak(self):
        return "Woof!"

animals = [Cat(), Dog()]
for animal in animals:
    print(animal.speak())

Output:

Meow!
Woof!

Summary 

Concept Description
Class A blueprint for objects
Object An instance of a class
Inheritance A class inherits from another class
Encapsulation Restrict access to data
Polymorphism Same method name, different behavior

Now you’re an OOP master in Python! 

 

Post a Comment

0 Comments