Nearby lessons
87 of 108Python - Instance Variables
What Are Instance Variables?
Instance variables are variables whose values can change from object to object.
Each object gets its own copy of instance variables.
Where Can They Be Declared?
- Inside the constructor using
self - Inside an instance method using
self - Outside the class using object reference
Example
class Employee:
def __init__(self):
self.eno = 100
self.ename = 'Durga'
self.esal = 10000Deleting and Accessing
We can access instance variables using self inside the class and object reference outside the class.
We can delete them using del self.variableName or del object.variableName.
Summary
| Topic | Meaning |
|---|---|
| Instance variable | Variable tied to a specific object |
| self | Used to access object data |
| del | Deletes instance variables |
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3
Keep Going!Python - Static Methods