5th Module Plc
5th Module Plc
🛠️How to Apply?
class Customer:
pass
cust1 = Customer()
2. Attributes
✅ What it is?
Attributes are variables that belong to an object or class.
🛠️How to Apply?
class Car:
def __init__(self, make, speed):
self.make = make
self.speed = speed
🛠️How to Apply?
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
r = Rectangle(5, 10)
print(r.width * r.height)
🛠️How to Apply?
c1 = create_customer("Ravi", 35)
print(c1.name, c1.age)
🛠️How to Apply?
class BankAccount:
def __init__(self, balance):
self.balance = balance
acc = BankAccount(1000)
acc.balance += 500 # Deposit
print(acc.balance) # Output: 1500
6. Copying Objects
✅ What it is?
Copying can be shallow (new reference) or deep (new object with copied values)
🛠️How to Apply?
import copy
class Car:
def __init__(self, brand, speed):
self.brand = brand
self.speed = speed
🔄 Summary Table
Concept Use Case - Automotive Use Case - Banking
Class/Object Vehicle, Engine, Sensor Customer, Account, Transaction
Attributes speed, fuel_type, engine_on balance, IFSC, account_type
Rectangle Parking area, sensor zone ATM screen, branch layout
Instances as Return create_engine(), build_car() open_account(), new_customer()
1. Time Class
✅ What it is?
A class used to represent time as an object with attributes like hour, minute, and
second.
🛠️How to Apply?
class Time:
def __init__(self, hour=0, minute=0, second=0):
self.hour = hour
self.minute = minute
self.second = second
def print_time(self):
print(f"{self.hour:02d}:{self.minute:02d}:{self.second:02d}")
🔷 2. Pure Functions
✅ What it is?
Functions that do not modify objects passed to them.
They take objects as input, perform computations, and return new objects without altering
the original.
🔷 3. Modifiers (Mutators)
✅ What it is?
Functions that modify the object passed to them, typically through self.
🛠️How to Apply?
🧠 Summary Table
Concept Description Automotive Example Banking Example
Represents hours,
Time Class Drive duration, sensor logs Session time, transaction logs
minutes, seconds
1. Object-Oriented Features
✅ What it is?
OOP is a programming paradigm based on objects. Python supports:
Encapsulation: Bundling data and methods.
Abstraction: Hiding implementation details.
Inheritance: Reusing code via parent-child relationships.
Polymorphism: Different classes respond to same method.
📍When/Where to Apply?
Automotive: Design Vehicle, Car, Truck classes.
🛠️How to Apply?
class Vehicle:
def start(self):
print("Vehicle starting...")
class Car(Vehicle):
def start(self):
print("Car starting with key...")
v = Vehicle()
c = Car()
v.start() # Vehicle starting...
c.start() # Car starting with key...
📍Where to Apply?
For debugging/logging object state.
🛠️How to Apply?
class Customer:
def __init__(self, name, balance):
self.name = name
self.balance = balance
def __str__(self):
return f"Customer: {self.name}, Balance: ₹{self.balance}"
def __str__(self):
return f"{self.type} of ₹{self.amount}"
t = Transaction(1000, "Deposit")
print(t)
🛠️Automotive Example:
class Engine:
def __init__(self, power):
self.power = power
def __str__(self):
return f"{self.power} HP Engine"
def __str__(self):
return f"{self.brand} with {self.engine}"
e = Engine(150)
c = Car("Volvo", e)
print(c) # Volvo with 150 HP Engine
📍When to Use?
When initializing object attributes.
class Account:
def __init__(self, acc_no, balance):
self.acc_no = acc_no
self.balance = balance
def __str__(self):
return f"Sensor ID: {self.id}, Value: {self.value}"
s = Sensor("TMP101", 36.6)
print(s)
🛠️Banking Example:
class Money:
def __init__(self, amount):
self.amount = amount
def __str__(self):
return f"₹{self.amount}"
m1 = Money(1000)
m2 = Money(500)
m3 = m1 + m2
print(m3) # ₹1500
🔷 8. Type-Based Dispatch
✅ What it is?
Different logic based on argument type.
📍Example:
def display(info):
if isinstance(info, str):
print("Name:", info)
elif isinstance(info, int):
print("Account Number:", info)
display("Vishwa")
display(123456)
🔷 9. Polymorphism
✅ What it is?
Different classes implement the same method in different ways.
📍Where to Use?
Banking: Different accounts have calculate_interest().
class SavingsAccount:
def calculate_interest(self):
return "Interest at 4%"
🛠️Example:
class Vehicle:
def start(self): # Interface
raise NotImplementedError
class Car(Vehicle):
def start(self): # Implementation
print("Car starts with a push button.")
class Truck(Vehicle):
def start(self):
print("Truck starts with ignition key.")
🧠 Summary Table
Concept Description Automotive Use Banking Use
Account(acc_no,
__init__() Initializes object Engine(power)
balance)
Customer,
__str__() String representation Sensor, Car
Transaction
Operator Distance +
Redefining +, ==, etc. Money + Money
Overloading Distance
Behavior based on log_data(sensor) display(info)
Type Dispatch
argument type
Polymorphism Shared interface, start() in Car, calculate_interest()