Nearby lessons

87 of 108

Python - 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 = 10000

Deleting 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 variableVariable tied to a specific object
selfUsed to access object data
delDeletes instance variables

🧠 Test Your Knowledge

3 Questions

Progress: 0 / 3
Keep Going!Python - Static Methods