Hello
Variables in OOP
Definition
In Object-Oriented Programming (OOP), variables are categorized based on how and where they are declared. The two main types of variables in Python OOP are:
Instance Variables
- Class Variables (Static Variables)
Hello
Hello
Instance Variables
Definition
Instance variables are variables that are unique to each object. They are defined inside the constructor (__init__) or other instance methods using self.
Where to define?
Inside __init__() using self.varname.
Key Points:
- Belong to the object (instance), not the class.
- Every object has its own copy.
- Can be modified per object.
Syntax:
class Student:
def __init__(self, name, age):
self.name = name # instance variable
self.age = age # instance variable
# Creating objects
s1 = Student("Mayank", 14)
s2 = Student("Aarul", 9)
print(s1.name, s1.age) # Mayank 14
print(s2.name, s2.age) # Aarul 9
Hello
Hello
Class Variables
Definition
Class variables are variables that are shared across all objects. They are defined inside the class but outside any instance method, typically at the top of the class body.
Where to define?
Directly inside the class (not in any method).
Key Points:
- Belong to the class, not individual objects.
- Shared among all instances.
- If modified using class name → affects all.
- If modified using self, it creates a new instance variable for that object.
Syntax:
class Student:
school_name = "DPS" # class variable
def __init__(self, name):
self.name = name
s1 = Student("Mayank")
s2 = Student("Aarul")
print(s1.school_name) # DPS
print(s2.school_name) # DPS
Student.school_name = "KV"
print(s1.school_name) # KV
print(s2.school_name) # KV
Hello
Hello
Difference Table: Instance vs Class Variable
Hello
Instance Variable
- Defined inside the __init__() method or other instance methods using self
- Belongs to each object individually.
- Each object gets its own memory copy of instance variables.
- Changing the variable affects only that particular object.
- Accessed using self.varname.
Class Variable
- Defined directly inside the class, outside of any method.
- Belongs to the class, and is shared by all instances.
- Memory is not duplicated per object — all share the same variable.
- Changing the variable using class name affects all objects.
- Accessed using ClassName.varname or self.varname (if not shadowed).
Hello
Hello
Modifying Class Variable from Instance
Careful
When you modify a class variable via self, it creates a new instance variable instead of modifying the original.
Syntax:
class Student:
school = "DPS" # class variable
def __init__(self, name):
self.name = name
s1 = Student("Mayank")
s1.school = "KV" # creates instance variable, doesn't change class variable
print(s1.school) # KV (instance-level)
print(Student.school) # DPS (unchanged)
How
Accessing and Updating Class Variables Properly
Syntax:
class Student:
school = "DAV"
@classmethod
def change_school(cls, new_name):
cls.school = new_name
Student.change_school("KV")
print(Student.school) # KV