Types of Class Variables in Python
1. Instance Variable
Definition:
- Instance variables are specific to each object.
- They are defined using the self keyword inside the constructor (__init__ method).
- Each object has its own separate copy of instance variables.
Program:
Output :
2. Class Variable:
Definition:
- Class variables are shared among all objects of the class.
- They are defined outside the constructor and are not prefixed with self.
- Changing a class variable affects all objects of that class.
Program:
Output :
0 Comments