Nearby lessons
102 of 159Python - self Variable
📌 What You Will Learn
- Understand what self represents inside a class
- Know where self is used: constructors and instance methods
- Access instance variables and methods using self
- Remember that self must always be the first parameter
What Is self?
self is the reference to the current object inside a class.
It is used to access instance variables and instance methods.
Where Is It Used?
- Inside constructors
- Inside instance methods
Example
Inside the constructor, self.name stores the name in the current object, so each object keeps its own value.
Important Note
self should always be the first parameter in instance methods and constructors.
Without self, Python cannot know which object the method is working with.
Summary
| Term | Meaning |
|---|---|
| self | Current object |
| Instance variable | Variable tied to object |
| Instance method | Method that uses self |
📝 Key Takeaways
- self is the reference to the current object inside a class
- It is used to access instance variables and instance methods
- self is used inside constructors and inside instance methods
- It must always be the first parameter of an instance method or constructor
🧠 Test Your Knowledge
6 QuestionsProgress: 0 / 6