What is Object-Oriented Programming?#
So far you've written programs as a series of functions working on data. Object-Oriented Programming (OOP) organises code around objects - bundles that contain both data (attributes) and behaviour (methods).
Real-world example - a car:
- Attributes (data): brand, colour, speed, fuel
- Methods (behaviour): start(), accelerate(), brake()
OOP is used in almost every large Python project - Django models, game characters, GUI windows, bank accounts, etc.
Class and object#
- A class is a blueprint (design / template).
- An object is an instance made from that blueprint.
One class Car → many objects: my_car, your_car, taxi.
Class = नक्शा (blueprint), Object = उस नक्शे से बनी असली चीज़।
जैसे एक मकान का नक्शा (class) एक बार बनता है, लेकिन उससे कई मकान (objects) बनाए जा सकते हैं। हर मकान का रंग, मालिक, पता अलग हो सकता है (attributes), लेकिन ढाँचा एक जैसा होता है।
Python में: class Student: नक्शा है, और s1 = Student("Asha"), s2 = Student("Ravi") उस नक्शे से बने दो अलग-अलग students (objects) हैं।
Defining a class#
Class names use CamelCase by convention: Student, BankAccount, ShoppingCart.
The constructor: __init__#
__init__ is a special method that runs automatically when an object is created. Use it to set the starting attributes:
Understanding self#
self is the object itself. When you write s1.method(), Python actually calls Student.method(s1) - so inside the method, self is s1.
self.name = namemeans "storenamein this object".- Every instance method must have
selfas its first parameter (you don't pass it - Python does).
self का मतलब है "यह वाला object"।
जब आप s1 = Student("Asha", "Python") लिखते हैं, तो __init__ के अंदर self का मतलब s1 होता है। इसलिए self.name = name का अर्थ है "Asha नाम को इस object (s1) के अंदर रख दो।" जब s2 बनता है, तब self का मतलब s2 होता है।
इसी वजह से हर object अपना अलग data रखता है। self method की पहली parameter होती है, पर call करते समय आपको उसे pass नहीं करना पड़ता - Python खुद करता है।
Methods - functions inside a class#
Methods can call other methods of the same object using self.method().
Instance attributes vs class attributes#
- Instance attributes (
self.x = ...in methods) - separate for each object. - Class attributes (defined directly in the class body) - shared by all objects.
Never use a mutable class attribute (like a list) for per-object data:
class Cart:
items = [] # shared by ALL carts - a common bug
Create it in __init__ instead: self.items = [].
Changing and checking attributes#
asha.name = "Asha Verma" # update
asha.city = "Indore" # add a new attribute (allowed, but better in __init__)
print(hasattr(asha, "city")) # True
print(getattr(asha, "phone", "N/A"))
print(vars(asha)) # all instance attributes as a dict
A complete example: shopping cart#
Why use OOP?#
| Benefit | Meaning |
|---|---|
| Organisation | data and the functions that use it live together |
| Reusability | create many objects from one class; extend classes (inheritance - next lesson) |
| Modelling | code mirrors real things: Student, Invoice, Product |
| Maintenance | change the class once, every object benefits |
Summary#
- A class is a blueprint; an object is an instance:
obj = ClassName(args). __init__(self, ...)sets up each new object;selfis the current object.- Instance attributes (
self.x) are per-object; class attributes are shared. - Methods are functions inside a class and always take
selffirst. - Create mutable attributes (lists, dicts) inside
__init__.