PYTHON OOP & CONCEPT NOTES
1. Class & Object
A class is a blueprint for objects. Objects are created by calling the class like a function.
2. self parameter
self refers to the current instance and is used to access instance variables.
3. Four Pillars of OOP
Encapsulation, Abstraction, Inheritance, Polymorphism.
4. if-elif-else vs if-else chain
if-elif-else checks mutually exclusive conditions; multiple ifs all run independently.
5. try-except vs try-except-finally
finally always executes even if exception occurs.
6. File modes r, w, a
r=read, w=write truncate, a=append.
7. in operator
Checks membership.
8. Class vs instance variable
Class variable shared, instance variable unique.
9. Without with
File may remain open; risk of data loss.
10. Arithmetic operators
+, -, *, /, //, % basic math.
ADVANCED TOPICS
1. Mutability Trap
Mutable class variables are shared across instances. Fix: use instance variables.
2. MRO
Method resolution order decides which parent method is called first.
3. Polymorphism Pitfall
If child doesn’t override, parent method runs. staticmethod acts like normal function.
4. Walrus Operator
x := value assigns and returns value.
5. Exception Specificity
except Exception doesn't catch SyntaxError. Use specific exceptions.
6. File Iterator Trick
Files are iterators; line-by-line read is memory efficient.
7. Operator Precedence
Multiplication/division before addition/subtraction.
8. Duck Typing
Methods must exist; Python doesn’t check types.
9. @property Decorator
Makes method accessible like attribute. Without setter, property becomes read-only.