Vivek Kaushik
AboutBlogWorkMy Work Ethics
šŸ Python OOP & Advanced Core

šŸ Python OOP & Advanced Core

Created
Feb 14, 2026 08:17 AM
Tags

1. Classes & Objects

1.1 Basic Class Definition

class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): return f"Hello, I'm {self.name}"
Usage:
p = Person("Vivek", 28) print(p.greet())

šŸ”¹ Important: self

  • self represents the instance
  • Similar to this in C#
  • Must be explicitly written in method definition

2. Instance vs Class Variables

2.1 Instance Variables

class Person: def __init__(self, name): self.name = name
Each object gets its own copy.

2.2 Class Variables

class Person: species = "Human" # class variable
Shared across all instances.

3. Dunder (Magic) Methods

Special methods surrounded by __.

3.1 str vs repr

class Person: def __init__(self, name): self.name = name def __str__(self): return f"Person({self.name})"
  • __str__ → readable
  • __repr__ → developer-focused representation

3.2 eq (Equality)

def __eq__(self, other): return self.name == other.name

3.3 len

def __len__(self): return len(self.items)

4. Inheritance

class Animal: def speak(self): return "Sound" class Dog(Animal): def speak(self): return "Bark"

4.1 super()

class Dog(Animal): def __init__(self, name): super().__init__() self.name = name

5. Multiple Inheritance

Python supports it.
class A: pass class B: pass class C(A, B): pass
Method Resolution Order (MRO):
C.__mro__
Interview note:
Python uses C3 linearization for method resolution.

6. Dataclasses (Very Important)

Cleaner way to create data objects.
from dataclasses import dataclass @dataclass class Person: name: str age: int
Auto-generates:
  • init
  • repr
  • eq
Very useful in APIs & AI pipelines.

7. Exception Handling

7.1 Try / Except

try: x = 1 / 0 except ZeroDivisionError: print("Cannot divide by zero")

7.2 Multiple Exceptions

except (ValueError, TypeError): pass

7.3 Finally

finally: print("Always executes")

7.4 Custom Exception

class MyError(Exception): pass

8. Modules & Packages

8.1 Importing

import math from math import sqrt

8.2 name == "main"

if __name__ == "__main__": main()
Prevents execution when imported.

9. Virtual Environments

Create:
python -m venv venv
Activate:
Mac/Linux:
source venv/bin/activate
Windows:
venv\Scripts\activate
Install packages:
pip install requests

10. File Handling

10.1 Reading File

with open("file.txt", "r") as f: content = f.read()

10.2 Writing File

with open("file.txt", "w") as f: f.write("Hello")

11. Context Managers

Used with with.
Built-in example:
with open("file.txt") as f: data = f.read()

11.1 Custom Context Manager

class MyContext: def __enter__(self): print("Enter") return self def __exit__(self, exc_type, exc_value, traceback): print("Exit")

12. Decorators (Beginner Level)

Decorators wrap functions.
def logger(func): def wrapper(): print("Before") func() print("After") return wrapper @logger def say_hello(): print("Hello")

13. Closures

Function inside function remembering outer variable.
def outer(): x = 10 def inner(): return x return inner

14. Memory Model & GIL (Interview Critical)

14.1 Reference Counting

Python tracks number of references to objects.
When count reaches zero → garbage collected.

14.2 Garbage Collection

Handles cyclic references.

14.3 Global Interpreter Lock (GIL)

  • Only one thread executes Python bytecode at a time.
  • Limits CPU-bound multithreading.
  • Does NOT affect I/O-bound tasks.
Interview-ready answer:
Python threads are good for I/O-bound tasks but not ideal for CPU-bound tasks due to GIL.

šŸ”„ Quick C# vs Python Mental Comparison

Concept
C#
Python
Access modifiers
public/private
Convention-based (_var)
Interfaces
Explicit
Duck typing
Overloading
Yes
Not in same way
Threading
True parallel
GIL limits CPU parallelism
慤
慤
慤
Ā 
Table of Contents
1. Classes & Objects1.1 Basic Class DefinitionšŸ”¹ Important: self2. Instance vs Class Variables2.1 Instance Variables2.2 Class Variables3. Dunder (Magic) Methods3.1 str vs repr3.2 eq (Equality)3.3 len4. Inheritance4.1 super()5. Multiple Inheritance6. Dataclasses (Very Important)7. Exception Handling7.1 Try / Except7.2 Multiple Exceptions7.3 Finally7.4 Custom Exception8. Modules & Packages8.1 Importing8.2 name == "main"9. Virtual Environments10. File Handling10.1 Reading File10.2 Writing File11. Context Managers11.1 Custom Context Manager12. Decorators (Beginner Level)13. Closures14. Memory Model & GIL (Interview Critical)14.1 Reference Counting14.2 Garbage Collection14.3 Global Interpreter Lock (GIL)šŸ”„ Quick C# vs Python Mental Comparison
Copyright 2026 Vivek Kaushik