Introduction to Object Oriented Programming in Python

Discover the basics of object-oriented programming (OOP) with Python in this informative tutorial. Learn how to define classes and objects, and how to use inheritance and polymorphism to create reusable, modular components.

Updated March 9, 2023

Hello, and welcome to Python Help!

Object-oriented programming (OOP) is a programming paradigm that is based on the concept of objects, which can contain data and code to manipulate that data. OOP allows you to organize your code into reusable, modular components, making it easier to maintain and extend your code over time. In this tutorial, we’ll introduce you to the basics of OOP in Python, and provide examples of how to use it in your own code.

Classes and Objects

In OOP, a class is a blueprint for creating objects, which are instances of the class. A class defines the data and behavior that an object of that class will have. To define a class in Python, you use the class keyword, followed by the name of the class and a colon. The body of the class contains the data and methods that define its behavior.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def say_hello(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

In this example, we define a Person class with a init method, which initializes the name and age attributes of the object. We also define a say_hello method, which prints a greeting to the console.

To create an object of the Person class, we use the class name followed by parentheses, like this:

person = Person("John", 30)

This creates a Person object with the name “John” and age 30.

Inheritance

Inheritance is a fundamental concept in OOP that allows you to create a new class by inheriting the properties and methods of an existing class. The new class is called a subclass, and the existing class is called the superclass.

class Employee(Person):
    def __init__(self, name, age, salary):
        super().__init__(name, age)
        self.salary = salary

    def get_salary(self):
        return self.salary

In this example, we define an Employee class that inherits from the Person class. The Employee class has an additional attribute, salary, and a method get_salary that returns the salary.

Polymorphism

Polymorphism is the ability of objects to take on different forms, depending on the context in which they are used. In Python, polymorphism is achieved through method overriding.

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

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

def animal_speak(animal):
    print(animal.speak())

dog = Dog()
cat = Cat()

animal_speak(dog) # outputs "Woof!"
animal_speak(cat) # outputs "Meow!"

In this example, we define a Dog class and a Cat class, both with a speak method. We also define a animal_speak function that takes an object as an argument and calls its speak method. This allows us to call animal_speak with both a Dog object and a Cat object, even though they are different classes with different implementations of the speak method.

Conclusion

That’s it for our introduction to object-oriented programming with Python. We hope you found this tutorial helpful and informative. Happy coding!

Hey! Do you love Python? Want to learn more about it?
Let's connect on Twitter or LinkedIn. I talk about this stuff all the time!