0% found this document useful (0 votes)
4 views3 pages

Essential Python OOP and Advanced Concepts

Uploaded by

pujashelake7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Essential Python OOP and Advanced Concepts

Uploaded by

pujashelake7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DETAILED PYTHON NOTES

1. What is a Class in Python? How do you create an object?

A class is a blueprint for creating objects. It defines attributes (variables) and methods (functions).

Example:

class MyClass:

def greet(self):

return "Hello"

obj = MyClass()

2. Purpose of self

self refers to the current object. It lets methods access instance variables and other methods.

3. Four Pillars of OOP

• Encapsulation – Hiding data. Example: private variables using _var.

• Abstraction – Showing only essential details. Example: abstract methods.

• Inheritance – One class acquiring another’s features. Example: class B(A).

• Polymorphism – Same method name, different behavior. Example: len("abc"), len([1,2]).

4. if-elif-else vs if-else chain

if-elif-else checks conditions sequentially and stops at FIRST match.

Multiple if’s execute ALL true conditions.

5. try-except vs try-except-finally

try-except handles errors.

finally ALWAYS executes (closing files, DB connection).

6. File Modes

r – read

w – write (overwrites)

a – append
7. in Operator

Checks membership.

Example: 3 in [1,2,3] → True

8. Class variable vs Instance variable

Class variable → shared by all objects.

Instance variable → unique per object.

9. Forgetting with statement

You must manually close file. Risk: memory leak, data not saved.

10. Arithmetic Operators

+ add, - subtract, * multiply, / divide, // floor division, % modulus.

---------------------------------------------

ADVANCED PYTHON TOPICS

1. Mutability Trap

If you use a list as class variable, all objects share it.

Fix: use list in __init__.

2. MRO (Method Resolution Order)

In multiple inheritance: order follows C3 linearization.

Use [Link]().

3. Polymorphism Pitfall

If child does not override, parent method runs.

@staticmethod does not use self.

4. Walrus Operator

x := 5 assigns and returns 5.

Used in loops to avoid duplicate function calls.


5. Exception Specificity

except Exception does NOT catch SyntaxError.

Use specific exceptions like ValueError.

6. File Iterator Trick

for line in file uses internal iterator, memory efficient even for 10GB.

7. Operator Precedence

2 + 3 * 4 // 2 - 1 → 2 + 12 // 2 - 1 → 2 + 6 - 1 → 7

(2 + 3) * (4 // 2) - 1 → 5 * 2 - 1 → 9

8. Duck Typing Edge

You can call quack() only if the class has quack().

Polymorphism depends on METHOD availability, not class type.

9. @property Decorator

Allows attribute-style access.

Without @[Link], you cannot modify the property value.

You might also like