Class vs Instance Attributes
Object-Oriented Programming (OOP) in Python revolves around classes and objects. One of the most important concepts in class design is understanding the difference between Class Attributes and Instance Attributes.
What are Class Variables?
- Class Variables are variables that are shared by all objects (instances) of a class.
- They belong to the class itself, not to individual objects.
- All objects access the same memory location for class variables.
Simple Definition
A class variable is defined inside the class but outside all methods, and it is shared by every instance of the class.
Syntax
class ClassName:
class_variable = valueExample
class Compnay:
company_name = "ABC"
# here,
# company_name is a class variableExample of Class Variables
class Employee:
compnay = "ABC PVT. LID."
def __init__(self,name, salary):
self.name = name
self.salary = salary
emp_1 = Employee("Rahul",25000)
emp_1 = Employee("Priya",15000)
print(emp1.company)
print(emp2.company)
# output:
# ABC PVT. LID.
# ABC PVT. LID.
# here,
# Both emp1 and emp2 share the same company variable.Memory Structure of Class Variables
Class (Employee)
│
├── company = "ABC PVT. LID."
│
├── Object emp1
│ ├── name = Rahul
│ └── salary = 25000
│
└── Object emp2
├── name = Priya
└── salary = 15000company variable is stored once in class memory
All objects refer to it.
Accessing Class Variables
There are two ways.
1. Using Class Name (Recommended)
print(Employee.company)2. Using Object
print(emp1.company)Modifying Class Variables
Class variables can be changed in two ways.
1. Modify using Class Name
class Employee:
company = "ABC PVT. LID."
Employee.company = "CDE PVT. LID."
print(Employee.company)2. Modify using Object
emp1.company = "Amazon"Important:
- This does not modify the class variable.
- Instead it creates a new instance variable for that object.
Example:
class Employee:
company = "ABC"
emp1 = Employee()
emp2 = Employee()
emp1.company = "DEF"
print(emp1.company)
print(emp2.company)
# output:
# ABC
# DEFWhat are Instance Variables?
Instance variables are variables that belong to individual objects (instances) of a class.
Get Saurabh Gandhi’s stories in your inbox
Join Medium for free to get updates from this writer.
Each object gets its own copy of these variables.
Simple Definition
Instance variables are variables defined inside a method (usually __init__) using self and they store data specific to each object.
Syntax
class ClassName:
def __init__(self):
self.variable_name = valueExample:
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
# here,
# self.name
# self.age
# are instance variables.Example
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
emp1 = Employee("Rahul", 50000)
emp2 = Employee("Priya", 70000)
print(emp1.name, emp1.salary)
print(emp2.name, emp2.salary)Memory Structure of Instance Variables
Class: Employee
│
├── Object emp1
│ ├── name = Rahul
│ └── salary = 50000
│
└── Object emp2
├── name = Priya
└── salary = 70000Instance variables are stored separately for each object
Where Can Instance Variables Be Created?
Instance variables can be created in three places.
1. Inside Constructor (__init__)
class Student:
def __init__(self, name):
self.name = name2. Inside Instance Methods
class Student:
def set_age(self, age):
self.age = age3. Outside the Class (Using Object)
class Student:
pass
s1 = Student()
s1.name = "Rahul"Accessing Instance Variables
Instance variables are accessed using object reference.
class Car:
def __init__(self, brand):
self.brand = brand
c1 = Car("BMW")
print(c1.brand)Modifying Instance Variables
Instance variables can be modified easily.
class Student:
def __init__(self, name):
self.name = name
s1 = Student("Rahul")
s1.name = "Amit"
print(s1.name)Deleting Instance Variables
You can delete them using del.
class Student:
def __init__(self, name):
self.name = name
s1 = Student("Rahul")
del s1.nameQuestions
- What is a Class Variable in Python?
- What is an Instance Variable?
- How do you access a class variable?
- How do you create a class in Python?
- How do you access an instance variable?
- How many copies of class variables exist?









