Python Introduction to OOP
Table of Contents
In the last lesson, we learned about Python Context Managers. After learning about functions and file handling, let us learn about Object Oriented Programming in Python. OOP is a way of writing programs that makes them easier to manage as they grow.
In this tutorial, we will learn what OOP is, why we need it, and the basic concepts behind it.
What Is Object Oriented Programming?
Object Oriented Programming is a way of writing code where we group data and actions together into something called objects.
Think of it like this. In real life, we deal with objects all the time. A car is an object. It has data (color, speed, brand) and actions (start, stop, accelerate). A bank account is an object. It has data (balance, owner) and actions (deposit, withdraw).
OOP lets us model these real-world things in our code. We create objects that have both data and behavior.
Why Do We Need OOP in Python?
As our programs grow, they become harder to manage. We have many variables and functions scattered everywhere. It becomes messy.
OOP helps us organize our code. We group related data and functions together. That makes our code easier to understand, easier to fix, and easier to extend.
Also, OOP lets us reuse code. We can create a basic object and then build more specific objects from it. That saves time and reduces mistakes.
The Main Concepts of OOP
OOP has four main concepts.
Class β A blueprint for creating objects. It defines what data and actions the objects will have.
Object β An instance of a class. It is the actual thing we create from the blueprint.
Encapsulation β Keeping data and methods together, and hiding internal details.
Inheritance β Creating new classes based on existing ones.
Polymorphism β Using the same method name for different types.
Abstraction β Hiding complex details and showing only what is needed.
We will learn each of these in detail in the coming tutorials.
A Simple Example of OOP in Python
Letβs create a simple class to see how it works. We will make a class called Dog. It will have a name and a method to bark.
class Dog: def __init__(self, name): self.name = name
def bark(self): print(f"{self.name} says Woof!")
my_dog = Dog("Buddy")my_dog.bark()Output
Buddy says Woof!Let me explain what happened.
- We defined a class called
Dog. - Inside the class, we defined a method called
__init__. That is the constructor. It runs when we create a new object. - The
selfparameter refers to the object itself. - We defined a method called
barkthat prints a message. - We created an object called
my_dogfrom theDogclass. - We called the
barkmethod on that object.
That is the basic idea of OOP. We create a blueprint (class), then create objects from it.
Class vs Object in Python
A class is like a blueprint. An object is like a house built from that blueprint.
We can create many objects from one class. Each object has its own data but shares the same methods.
dog1 = Dog("Buddy")dog2 = Dog("Max")
dog1.bark()dog2.bark()Output
Buddy says Woof!Max says Woof!Both dogs can bark. But they have different names. That is the power of objects.
Why Is OOP Useful?
OOP makes our code organized. Instead of having many separate variables and functions, we group them together. That makes our code easier to read and maintain.
OOP also makes it easy to reuse code. We can create a base class and then extend it for specific needs. That saves time and reduces mistakes.
And OOP matches how we think about the real world. We naturally think in terms of objects. So OOP feels natural.
Common Mistakes in OOP for Beginners
Mistake 1: Forgetting self
class Dog: def bark(): print("Woof!")This gives an error when we call bark(). Because methods need self as the first parameter.
Fix:
class Dog: def bark(self): print("Woof!")Mistake 2: Forgetting to create an object
class Dog: def bark(self): print("Woof!")
Dog.bark()This gives an error. Because we are calling the method on the class, not on an object.
Fix: Create an object first.
my_dog = Dog()my_dog.bark()Mistake 3: Using the wrong case for class names
Class names should use PascalCase (capitalize each word). Not snake_case.
Good:
class Dog: pass
class BankAccount: passBad:
class dog: pass
class bank_account: passNext, we will learn about Python Classes and Objects and create and use classes and objects.