OOP & Advanced Python

OOPs in Python

01 · What is OOP? File · OOPs · 1 of 15

What is Object-Oriented Programming?

OOP stands for Object-Oriented Programming.

It is a way of writing programs by representing real-world things as software objects.

Consider a mobile phone:

  • Data: brand, color, price, battery percentage
  • Actions: call, charge, take a photo

In OOP:

  • The data is represented using attributes
  • The actions are represented using methods
  • The blueprint is called a class
  • The actual mobile created from that blueprint is called an object
Python
class mobile:
  def call(self):
    print("calling someone.....")

phone = mobile()
phone.call()
Output
calling someone.....
A simple class with a method that an object can call.
02 · Class & Object File · OOPs · 2 of 15

Class and Object

What is a Class?

A class is a blueprint or template used to create objects.

A house plan is not an actual house. Multiple houses can be constructed using the same plan.

Similarly, a class is a plan, and objects are the actual items created from it.

Python
class student:
  name = "Manu"
  course = "GenAI"
Here student is a class.

What is an Object?

An object is an actual instance created from a class.

Python
student1 = student()
student2 = student()

print(student1.name)
print(student2.course)
Output
Manu
GenAI
student → class
student1 and student2 → objects
name and course → attributes
03 · Attributes & Methods File · OOPs · 3 of 15

Attributes and Methods

An attribute stores information about an object.

A method defines an action the object can perform.

Python
class Bankaccount:
  account_holder = "Sundar"
  balance = 5000

  def check_balance(self):
    print("balance:", self.balance)

account = Bankaccount()
print(account.account_holder)
account.check_balance()
Output
Sundar
balance: 5000
Bankaccount is the class
account is an object
account_holder and balance are attributes
check_balance is a method
04 · Constructor (__init__) File · OOPs · 4 of 15

Constructor and self

A constructor is a special method that automatically runs when an object is created from a class.

In Python, the constructor method is written as __init__().

Python
class Employee:
  def __init__(self):
    print("Constructor executed")

employee1 = Employee()
Output
Constructor executed
We never called __init__() directly. It ran automatically when the object was created.

Without Constructor

Python
class Employee:
  pass

employee1 = Employee()
employee1.name = "Prashant"
employee1.salary = 50000
We must assign every value separately.

With Constructor

Python
class Employee:
  def __init__(self, name, salary):
    self.name = name
    self.salary = salary

employee1 = Employee("manu", 50000)
The constructor ensures every object is created with the necessary information.
05 · Understanding self File · OOPs · 5 of 15

Understanding self

self represents the current object.

name and salary are constructor parameters.

self.name and self.salary are instance variables stored in the object.

When we write:

Python
employee1 = Employee("manu", 50000)

Python internally performs something similar to:

Python
Employee.__init__(employee1, "manu", 50000)

We do not pass a value for self. Python passes the object automatically.

06 · Parameterized Constructor File · OOPs · 6 of 15

Parameterized Constructor

A constructor that accepts values is called a parameterized constructor.

Python
class Bankaccount:
  def __init__(self, account_number, customer_name, balance):
    self.account_number = account_number
    self.customer_name = customer_name
    self.balance = balance

  def display_details(self):
    print("Account:", self.account_number)
    print("customer:", self.customer_name)
    print("Balance:", self.balance)

account1 = Bankaccount(1001, "ravi", 50000)
account1.display_details()

account2 = Bankaccount(1002, "priya", 60000)
account2.display_details()
Output
Account: 1001
customer: ravi
Balance: 50000
Account: 1002
customer: priya
Balance: 60000
Each object gets its own values through the parameterized constructor.
07 · Instance Variables File · OOPs · 7 of 15

Instance Variables

Instance variables belong to an individual object.

Each object receives its own copy of instance variables.

Python
class Employee:
  def __init__(self, name, salary):
    self.name = name
    self.salary = salary

employee1 = Employee("Nandhini", 80000)
employee2 = Employee("Keerthana", 9000)

print(employee1.salary)
print(employee2.salary)
Output
80000
9000
Each object has its own separate copy of name and salary.
08 · Instance Methods File · OOPs · 8 of 15

Instance Methods

An instance method is a method that works with the data of a specific object.

It always takes self as the first parameter.

Most methods you write in a class are instance methods.

Python
class Employee:
  def __init__(self, name, salary):
    self.name = name
    self.salary = salary

  def show_details(self):          # Instance Method
    print("Name:", self.name)
    print("Salary:", self.salary)

emp1 = Employee("Ravi", 50000)
emp1.show_details()
Output
Name: Ravi
Salary: 50000
show_details is an instance method because it uses self to access the data of a particular object.
09 · Class Methods File · OOPs · 9 of 15

Class Methods

A class method works with the class itself, not with individual objects.

It is created using the @classmethod decorator and takes cls as the first parameter.

Python
class Employee:
  company = "KSR Datavizon"     # Class Variable

  def __init__(self, name, salary):
    self.name = name
    self.salary = salary

  @classmethod
  def change_company(cls, new_name):
    cls.company = new_name
    print("Company name changed to:", cls.company)

Employee.change_company("KSR Tech")
print(Employee.company)
Output
Company name changed to: KSR Tech
KSR Tech
Class methods are useful when you want to work with class variables or create alternative constructors.
10 · Static Methods File · OOPs · 10 of 15

Static Methods

A static method does not work with instance data (self) or class data (cls).

It is just a normal function that lives inside a class for organizational purposes.

It is created using the @staticmethod decorator.

Python
class Calculator:

  @staticmethod
  def add(a, b):
    return a + b

  @staticmethod
  def multiply(a, b):
    return a * b

print(Calculator.add(10, 20))
print(Calculator.multiply(5, 6))
Output
30
30
Static methods are used when the logic belongs to the class conceptually, but does not need access to instance or class data.
11 · Single Inheritance File · OOPs · 11 of 15

Single Inheritance

One child class inherits from one parent class.

Python
class Animal:
  def eat(self):
    print("Eating food")

class Dog(Animal):
  def bark(self):
    print("Woof!")

dog = Dog()
dog.eat()
dog.bark()
Output
Eating food
Woof!
Dog inherits the eat() method from Animal.
12 · Multi-level Inheritance File · OOPs · 12 of 15

Multi-level Inheritance

A class inherits from a child class, forming a chain.

Python
class Employee:
  def attend_meeting(self):
    print("Attending a meeting")

class Developer(Employee):
  def write_code(self):
    print("Writing code")

class seniorDeveloper(Developer):
  def review_code(self):
    print("Reviewing code")

senior = seniorDeveloper()
senior.attend_meeting()
senior.write_code()
senior.review_code()
Output
Attending a meeting
Writing code
Reviewing code
13 · Hierarchical Inheritance File · OOPs · 13 of 15

Hierarchical Inheritance

Multiple child classes inherit from the same parent class.

Python
class Employee:
  def login(self):
    print("Logged in")

class Developer(Employee):
  def write_code(self):
    print("Writing code")

class Manager(Employee):
  def manage_team(self):
    print("Managing the team")
Both Developer and Manager inherit login().
However, a Manager does not automatically inherit write_code() from Developer. They are separate child classes.
14 · Multiple Inheritance File · OOPs · 14 of 15

Multiple Inheritance

A class can inherit from more than one parent class.

Python
class camera:
  def take_photo(self):
    print("Taking a photo")

class musicplayer:
  def play_music(self):
    print("Playing music")

class Smarthphone(camera, musicplayer):
  pass

phone = Smarthphone()
phone.play_music()
phone.take_photo()
Output
Playing music
Taking a photo
Smarthphone inherits from both camera and musicplayer.
15 · Method Resolution Order File · OOPs · 15 of 15

Method Resolution Order (MRO)

What if both parent classes have the same method?
Python follows the Method Resolution Order (MRO).

Python
class A:
  def show(self):
    print("Method from A")

class B:
  def show(self):
    print("Method from B")

class C(A, B):
  pass

obj = C()
obj.show()
print(C.__mro__)
Output
Method from A
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
Python looks for the method in the order defined in the class inheritance list.
Here it found show() first in class A.
Home OOPs in Python

Object-Oriented Programming in Python

Classes, Objects, Constructor, Attributes, Methods and Inheritance.

Select any topic from the left sidebar to begin.


Start Learning →