• May 2, 2025

Difference between Class and Instance variable

Class Variables are the same across all instances.

Instance Variables are unique to each instance.

class employee:
pay_raise = 1.10

def __init__(self, name, age,salary):
self.name = name
self.age = age

def pay_raise(self):
self.salary = int(self.salary * employee.pay_raise)
or
self.salary = int(self.salary * self.pay_raise) # this method has got more control

Note – When we access class variable through instance:
1. First check variable is available in the instance then,
2. check if it is available in class.

employee.pay_raise=1.20 -> overrides to 20% everywhere whereas

employee1.pay_raise=1.20 -> only applied to employee1 instance.

To test the attributes and methods that are there for employee1 instance:

print(employee1.__dict__)