BeginnerCoder
intermediate#python#oop#programming

Understanding Object-Oriented Programming in Python

This article was AI-drafted and has not been reviewed yet. Spotted a mistake? Tell us.

Learn the basics of object-oriented programming in Python, including classes, objects, inheritance, and polymorphism.

Introduction to OOP

Object-oriented programming (OOP) is a programming paradigm that revolves around the concept of objects and classes.

Classes and Objects

A class is a blueprint for creating objects, and an object is an instance of a class.

class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model
my_car = Car('Toyota', 'Corolla')
print(my_car.brand)

Inheritance

Inheritance is the process of creating a new class based on an existing class.

class ElectricCar(Car):
    def __init__(self, brand, model, battery_size):
        super().__init__(brand, model)
        self.battery_size = battery_size
electric_car = ElectricCar('Tesla', 'Model S', 75)
print(electric_car.brand)

Polymorphism

Polymorphism is the ability of an object to take on multiple forms.

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height
class Circle:
    def __init__(self, radius):
        self.radius = radius
    def area(self):
        return 3.14 * self.radius ** 2
rectangle = Rectangle(4, 5)
circle = Circle(3)
print(rectangle.area())
print(circle.area())
Keep going with python

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.