python-backend
/

Object-Oriented Programming (OOP) in Python

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

python-backend

Object-Oriented Programming (OOP) in Python

What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of 'objects', which can contain data (attributes) and code (methods). Python is a multi-paradigm language, but it is heavily built around OOP principles. Everything in Python—from strings to functions—is actually an object.

Classes and Objects

A Class is a blueprint for creating objects. An Object is an instance of a class. The __init__ method is the constructor that initializes an object's attributes when it is created.

PythonRead-only
1
class Developer:
    # Class attribute
    platform = "Mobile"

    def __init__(self, name, tech_stack):
        # Instance attributes
        self.name = name
        self.tech_stack = tech_stack

    def introduce(self):
        return f"Hi, I'm {self.name}, a {self.tech_stack} expert."

# Creating an instance (Object)
lead = Developer("Kishore", "Flutter")
print(lead.introduce())

The Four Pillars of OOP

  • Encapsulation: Bundling data and methods that work on that data within one unit and restricting access to some components (using underscores like _name or __name).
  • Inheritance: Allowing a class (child) to derive attributes and methods from another class (parent).
  • Polymorphism: Allowing different classes to be treated as instances of the same general class through the same interface (e.g., different objects having a render() method).
  • Abstraction: Hiding complex implementation details and showing only the necessary features of an object.

Inheritance Example

Inheritance allows for code reuse. You can use the super() function to call methods from the parent class.

PythonRead-only
1
class Person:
    def __init__(self, name):
        self.name = name

class Manager(Person):
    def __init__(self, name, team_size):
        super().__init__(name) # Call parent constructor
        self.team_size = team_size

    def manage(self):
        return f"{self.name} is managing a team of {self.team_size}."

OOP Comparison: Python vs. Dart

FeaturePythonDart (Flutter)
Constructor__init__(self, ...)ClassName(...)
Instance Referenceselfthis
Access ModifiersNaming convention (_ or __)Keywords (private is _)
Multiple InheritanceSupportedNot supported (uses Mixins)
InterfacesAbstract Base Classes (abc)Implicit (every class is an interface)

Try it yourself

class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.__balance = balance # Private attribute

    def deposit(self, amount):
        self.__balance += amount
        print(f"Added {amount}. New balance: {self.__balance}")

    def get_balance(self):
        return self.__balance

account = BankAccount("Kishore", 1000)
account.deposit(500)
print(f"Final Balance for {account.owner}: {account.get_balance()}")

Test Your Knowledge

Q1
of 3

Which method is called automatically when a new object is created?

A
__new__
B
__start__
C
__init__
D
__create__
Q2
of 3

How do you inherit a class in Python?

A
class Child(Parent):
B
class Child extends Parent:
C
class Child implements Parent:
D
class Child : Parent
Q3
of 3

What is the purpose of super()?

A
To make a class run faster
B
To call the parent class's methods or constructor
C
To define a static method
D
To terminate an object

Frequently Asked Questions

What is 'self' in Python?

The 'self' parameter is a reference to the current instance of the class and is used to access variables that belong to the class. It must be the first parameter of any function in the class.

What are 'Dunder' methods?

Dunder (Double Under) methods are special methods like init, str, or add that allow you to define how objects behave with built-in Python operations.

Does Python have private variables?

Technically, no. Using a single underscore (e.g., _name) is a hint that it's internal. Using double underscores (e.g., __name) triggers 'name mangling' to make it harder to access from outside, but it's not strictly private like in Java.

Previous

python functions

Next

python modules

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.