We have learned how to create classes and objects. Now, we will explore class variables and instance variables in Python, including how they are used and when to use each one. This chapter also explains how data is stored in variables. If you are still confused about these concepts, this chapter will help clarify them.
What is Variable?
A variable is like a container that stores data or a value. It can store different types of data, such as strings, integers, floats, booleans, and more.
name = "Somit"
age = 30
salary = 50000Here, name stores a string value, while age and salary store integer values.
Variable in Class and Difference
Inside a class, we use instance variables and class variables. An instance variable belongs to a specific object created from the class. Therefore, when we change an instance variable for one object, it does not affect the same variable in other objects.
While, a class variable is shared by all objects created from the class. It is used when we want to store data that should be common to every object of the class.
Example for Instance Variable
class Hospital:
def __init__(self, patient, bednumber):
self.patient = patient
self.bednumber = bednumber
WardA = Hospital("Ajay", 32)
WardB = Hospital("Vijay", 35)
print(WardA.patient)
print(WardA.bednumber)
print(WardB.patient)
print(WardB.bednumber)
WardA and WardB are objects (instances) of the Hospital class. The __init__ method is the constructor that initializes the instance variables.
In this example, patient and bednumber are instance variables because each object has its own values.
Example for Class Variable
class Hospital:
Hospital_Name = "Shaurya Hospital"
def __init__(self, patient, bednumber):
self.patient = patient
self.bednumber = bednumber
WardA = Hospital("Ajay", 32)
WardB = Hospital("Vijay", 35)
print(WardA.Hospital_Name)
print(WardB.Hospital_Name)Change Class Variable
Hospital.Hospital_Name ="Agam Hospital"
##Check object
print(WardA.Hospital_Name)
print(WardB.Hospital_Name)Changing a class variable affects all objects (instances) of that class that use the variable.
Quick Difference
| Instance Variable | Class Variable |
|---|---|
| Belongs to an object | Belongs to the class |
| Each object has its own copy | One copy shared by all objects |
Created using self.variable | Created directly inside the class |
| Changing one object doesn't affect others | Changing it affects all objects |
More Examples
class Restaurant:
Name_Restaurant = "Annapurna Restaurant"
def __init__(self, snacks, price):
self.snacks = snacks
self.price = price
Snack1 = Restaurant("Burger", 50)
Snack2 = Restaurant("Dosa", 100)
Snack3 = Restaurant("Idli", 30)
print(Snack3.Name_Restaurant)
print(Snack1.snacks)
print(Snack1.price)
Note: If the constructor has additional parameters, you must provide values for them when creating the object, unless those parameters have default values.
For example: def __init__(self, snacks, price, rating=0):
class Admissions:
College = "Chhatrapati Shahu Ji Maharaj University"
Total_count = 0
def __init__(self, name, course):
self.name = name
self.course = course
Admissions.Total_count += 1
Science1 = Admissions("Amit", "BSc.")
Science2 = Admissions("Rohit", "BSc.")
Science3 = Admissions("Priya", "Bsc.")
print(Admissions.Total_count)
Note: If we use the same object variable name again, the previous reference is replaced by the new object.
Conclusion
@classmethod)