Python OOPS

Object Oriented Programming Style
Agenda
-
What is OOPS ?
-
Classes
-
Objects
-
Real life example



What is OOPS?
Object Oriented Programming Style (OOPS) is a programming style that deals with classes & objects.
The main aim of object-oriented programming is to implement real-world entities, for example, object, classes, abstraction, inheritance, polymorphism, etc.
A class is a blueprint for the object.

What is a class ?
What is an object ?
What is a class ?
An object is an entity that has a state and some behaviour.

Car

Car
Blueprint

Blueprint

Blueprint
Factory

Blueprint
Factory

Blueprint

Blueprint

Blueprint
Properties:

Blueprint
Properties:
- Model name

Blueprint
Properties:
- Model name
- Color

Blueprint
Properties:
- Model name
- Color
- Price

Blueprint
Properties:
- Model name
- Color
- Price
Functionalities:

Blueprint
Properties:
- Model name
- Color
- Price
Functionalities:
- Drive

Blueprint
Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock
class Car:
# body

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock
class Car:
model = "Hatchback"
color = "Black"
price = 100000

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
Ignore the 'self' keyword for now. We'll cover it later.

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
Ignore the 'self' keyword for now. We'll cover it later.

class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")

class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")

class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
Blueprint

class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
Blueprint
# Main part of the program

Blueprint
# Main part of the program
c1 = Car()
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")

Blueprint
c1
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()

Blueprint
c1
c2
# Main part of the program
c1 = Car()
c2 = Car()
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")

Blueprint
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
c1
c2
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")

Blueprint
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000
c1
c2
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")

class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
model
color
price
Properties
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
model
color
price
Data members
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
model
color
price
drive()
lock()
unlock()
Data members
Functions
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
Hatchback
Black
100000
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
Hatchback
Black
100000
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
Hatchback
Black
100000
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
Hatchback
Black
100000
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000
c2.color = "Red"
print(c2.color) # Red

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
Hatchback
Red
100000
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000
c2.color = "Red"
print(c2.color) # Red

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
Hatchback
Red
100000
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000
c2.color = "Red"
print(c2.color) # Red
c2.fuel = "CNG"

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
fuel:
Hatchback
Red
100000
CNG
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000
c2.color = "Red"
print(c2.color) # Red
c2.fuel = "CNG"

class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
fuel:
Hatchback
Red
100000
CNG
class Car:
model = "Hatchback"
color = "Black"
price = 100000
def drive(self):
print("Zoom zoom zoom")
def lock(self):
print("Car is now locked")
def unlock(self):
print("Car is now unlocked")
# Main part of the program
c1 = Car()
c2 = Car()
print(c1.color) # Black
print(c1.model) # Hatchback
print(c1.price) # 100000
c2.color = "Red"
print(c2.color) # Red
c2.fuel = "CNG"
print(c2.fuel) # CNG
- The first argument of a method of a class is generally referred as 'self'.
- self refers to the calling object.
- This argument is passed implicitly by the Python interpreter and need not be mentioned while calling.

self keyword
class ComplexNumber:
real = 0
imaginary = 0
def print(self):
print("{} + {}i".format(self.real, self.imaginary))
# 5 + 3i
x = ComplexNumber()
x.real = 5
x.imaginary = 3
x.print()

Example: Complex Numbers
Write a class to store and perform operations on complex numbers.
- __init__() is a special method inside a class that allows us to define the properties/attributes while creating it.
- It is a special method that is called/invoked automatically when the object is created. The user need not call it.
- It is similar to "Constructors" in C++ and Java.

__init__() method

Everything is an Object
In Python, pretty much everything is an object, whether it is a number, a function or even modules.
a = 5
print(type(a)) # <class 'int'>
b = "May the Force be with you"
c = 63.29
def hello():
print("Hello world")
print(type(b)) # <class 'str'>
print(type(c)) # <class 'float'>
print(type(hello)) # <class 'function'>

Extra Reading Material

Python OOPS
By Tarun Luthra
Python OOPS
- 618