Zone Of Makos

Menu icon

OOPS in Python

Object-Oriented Programming (OOP) is a programming paradigm that uses objects to represent and manipulate data. Python is an object-oriented programming language, which means it has built-in support for creating and using objects. In this lesson, we'll explore the basics of OOP in Python and how it can be used to write clean, modular, and reusable code.

Classes and Objects in Python

A class is a blueprint for creating objects, which defines the properties and methods that the objects will have. An object is an instance of a class, which contains data and functions that operate on that data. In Python, you can create a class using the class keyword, and create an object using the class name followed by parentheses. Let's take a look at an example:


# Define a class
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    
    def drive(self):
        print("Driving the car")
    
    def stop(self):
        print("Stopping the car")

# Create an object of the class
my_car = Car("Toyota", "Corolla", 2022)

# Access object properties and methods
print(my_car.make)
print(my_car.model)
print(my_car.year)
my_car.drive()

In this example, we define a Car class with properties for make, model, and year, as well as methods for driving and stopping the car. We then create an object of the class using the Car constructor, and access its properties and methods using dot notation.

Inheritance in Python

Inheritance is a key feature of object-oriented programming, which allows you to create a new class that is a modified version of an existing class. In Python, you can create a subclass by using the class keyword and specifying the base class in parentheses. The subclass inherits all the properties and methods of the base class, and can also add new properties and methods or modify existing ones. Let's take a look at an example:


# Define a base class
class Vehicle:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    
    def drive(self):
        print("Driving the vehicle")
    
    def stop(self):
        print("Stopping the vehicle")

# Define a subclass
class Car(Vehicle):
    def __init__(self, make, model, year, color):
        super().__init__(make, model, year)
        self.color = color
    
    def honk(self):
        print("Honking the horn")

# Create an object of the subclass
my_car = Car("Toyota", "Corolla", 2022, "Red")

# Access object properties and methods
print(my_car.make)
print(my_car.model)
print(my_car.year)
print(my_car.color)
my_car.drive()
my_car.honk()

In this example, we define a Vehicle base class with properties for make, model, and year, as well as methods for driving and stopping the vehicle. We then define a Car subclass that inherits from the Vehicle base class and adds a new property for color and a new method for honking the horn. We create an object of the Car subclass and access its properties and methods using dot notation.

Encapsulation in Python

Encapsulation is the concept of hiding the internal details of an object and only exposing a public interface for accessing and modifying the object's data. In Python, you can achieve encapsulation by using private variables and methods, which are denoted by a double underscore prefix. Private variables and methods can only be accessed from within the class definition, not from outside the class. Let's take a look at an example:


# Define a class with private variables and methods
class Person:
    def __init__(self, name, age):
        self.__name = name
        self.__age = age
    
    def __display_age(self):
        print("Age:", self.__age)
    
    def display_info(self):
        print("Name:", self.__name)
        self.__display_age()

# Create an object of the class
my_person = Person("Alice", 30)

# Access object properties and methods
my_person.display_info()
# Output: Name: Alice
#         Age: 30
print(my_person.__name)  # Raises an AttributeError
print(my_person.__display_age())  # Raises an AttributeError

In this example, we define a Person class with private variables for name and age, as well as a private method for displaying the age. We then define a public method for displaying the person's name and age, which calls the private method. We create an object of the class and access its public method, but we cannot access its private variables or methods from outside the class definition.

Polymorphism in Python

Polymorphism is the concept of using a single interface to represent multiple types of objects. In Python, you can achieve polymorphism by using inheritance and method overriding. Method overriding is the process of defining a new implementation for a method in a subclass, which has the same name and signature as the method in the base class. When you call the method on an object of the subclass, the new implementation is used instead of the one in the base class. Let's take a look at an example:


# Define a base class with a method
class Animal:
    def make_sound(self):
        pass

# Define a subclass with a different implementation of the method
class Dog(Animal):
    def make_sound(self):
        print("Woof")

# Define another subclass with a different implementation of the method
class Cat(Animal):
    def make_sound(self):
        print("Meow")

# Create objects of the subclasses and call the method
my_dog = Dog()
my_cat = Cat()

my_dog.make_sound()  # Output: Woof
my_cat.make_sound()  # Output: Meow

In this example, we define a Animal base class with a method for making a sound. We then define two subclasses, Dog and Cat , each with a different implementation of the method. We create objects of the subclasses and call the method, and the appropriate implementation is used for each object.

Conclusion

In this lesson, we've covered the basics of object-oriented programming in Python, including classes, objects, attributes, methods, inheritance, encapsulation, and polymorphism. These concepts are essential for building complex programs and applications, and they provide a powerful and flexible way to organize and structure your code. In the next lesson, we'll dive deeper into advanced topics in Python, including iterators, generators, closures, and decorators, and explore how these concepts can be used to write more concise and efficient code.