
๐ Python Mistakes Everyone Makes ❌
Day 8: Forgetting self in Class Methods
When learning object-oriented programming in Python, forgetting self is one of the most common (and confusing) mistakes.
❌ The Mistake
❌ Why this fails?
Because instance methods must accept self as the first parameter.
-
When you call p.greet(), Python automatically passes the instance (p) to the method.
-
Without self, Python doesn’t know where to put that argument, causing a TypeError.
✅ The Correct Way
๐ง Simple Rule to Remember
-
Instance methods → must have self
self represents the object calling the method
-
It allows access to instance variables and other methods
✅ Key Takeaway
Always include self as the first parameter in instance methods.
It’s how Python connects methods to the object they belong to.

0 Comments:
Post a Comment