DEV Community

Sajjad Rahman
Sajjad Rahman

Posted on

3 1 2

Basic OOP - Part 01

Everything in Python is Object-Oriented

Example:

a = 2  # 'a' is an object of integer type
Enter fullscreen mode Exit fullscreen mode

Similarly:

roll = [1, 2, 3, 4]
roll.append(5)  # list method
roll.pop()      # remove last element
Enter fullscreen mode Exit fullscreen mode

Here, roll is an object of the list class.


🧱 What is a Class?

A class is a blueprint. It has:

  1. Data / Properties → Variables
  2. Functions / Behaviors → Methods

Example (Structure only):

class Human:
    name
    age
    phone_no

    def demo():
        pass
Enter fullscreen mode Exit fullscreen mode
  • Class names should be in PascalCase → ThisIsClass
  • Variable and method names should be in snake_case → this_is_variable

💡 OOP Benefits

  1. ✅ Reusable code
  2. ✅ No global variables required
  3. ✅ Organized and modular code
  4. ✅ Easier debugging
  5. ✅ Data protection via encapsulation

🔁 Procedural vs Object-Oriented (OOP)

🧨 Non-OOP Example (Risky!):

balance = 100

def withdraw(amount):
    global balance
    balance -= amount
Enter fullscreen mode Exit fullscreen mode

✅ OOP Version (Cleaner and Safer):

class BankAccount:
    def __init__(self, balance):
        self.balance = balance

    def withdraw(self, amount):
        self.balance -= amount

# Usage
account1 = BankAccount(100)
account2 = BankAccount(200)

account1.withdraw(50)  # Only affects account1
Enter fullscreen mode Exit fullscreen mode

🔧 Class with Constructor (__init__())

A special method called when the object is created.

Example:

class Atm:
    def __init__(self):
        print("ATM object created")
Enter fullscreen mode Exit fullscreen mode

Note:

  • It's spelled __init__, not __int__
  • Called automatically when an object is instantiated

🎓 __init__() is a Magic Method

Magic methods in Python have double underscores: __method__.
You cannot skip __init__() if you want to initialize object attributes.


🍽 OOP Analogy — Like a Recipe!

OOP Concept Cooking Analogy
Class Recipe
Object Cooked Dish
Instantiation Cooking
Attributes Spice Level, Garnish
Methods Cooking Instructions

🍛 Custom Class Example: Dish

Define:

class Dish:
    def __init__(self, name, spice_level, garnish):
        self.name = name
        self.spice_level = spice_level
        self.garnish = garnish

    def serve(self):
        print(f"Serving {self.name} with {self.spice_level} spice and {self.garnish} garnish.")
Enter fullscreen mode Exit fullscreen mode

Create objects:

dish1 = Dish("Curry", "Medium", "Cilantro")
dish2 = Dish("Curry", "Spicy", "Mint Leaves")

dish1.serve()
dish2.serve()
Enter fullscreen mode Exit fullscreen mode

Output:

Serving Curry with Medium spice and Cilantro garnish.
Serving Curry with Spicy spice and Mint Leaves garnish.
Enter fullscreen mode Exit fullscreen mode

🙋 What is self?

  • Refers to the current object instance.
  • Always required as the first parameter in instance methods.

Example:

self.name = name  # means "this object’s name = the given name"
Enter fullscreen mode Exit fullscreen mode

🧬 Inheritance (Optional but Powerful)

Allows a class to inherit properties and methods from another.

class Dish:
    def __init__(self, name):
        self.name = name

class SweetDish(Dish):
    def serve(self):
        print(f"Serving sweet {self.name}")

cake = SweetDish("Cake")
cake.serve()  # Output: Serving sweet Cake
Enter fullscreen mode Exit fullscreen mode

🔐 Rest of the part add soon

NB : I collected those from AI Chatbot

$150K MiniMax AI Agent Challenge — Build Smarter, Remix Bolder, Win Bigger!

$150K MiniMax AI Agent Challenge — Build Smarter, Remix Bolder, Win Bigger!

Developers, innovators, and AI tinkerers, build your AI Agent and win $150,000 in cash

Signup Now

Top comments (1)

Collapse
 
abir17 profile image
abir

Carry on✅✅✅

Your Python stack deserves better infra

Your Python stack deserves better infra

Stop duct-taping user flows together. Manage auth, access, and billing in one simple SDK with Kinde.

Get a free account

👋 Kindness is contagious

Take a moment to explore this thoughtful article, beloved by the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A heartfelt "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay