Class variables are defined outside initializer method are shared with all objects.
(teamName as class variable shared across different objects)
But here I see private variables are defined as class variables outside initializer method .
The variables are not shared across objects.
Behavior of private variables and public variables defined as class variables are different.?
__name , __rollNumber are class variables but not shared across objects?
class Student: __name = None __rollNumber = None def setName(self, name): self.__name = name def getName(self): return self.__name def setRollNumber(self, rollNumber): self.__rollNumber = rollNumber def getRollNumber(self): return self.__rollNumber
demo1 = Student()
print(“Roll Number:”, demo1.getRollNumber())
demo2 = Student()
print(“Name- after:”, demo1.getName())
Ouput: Shows demo1 reference still hold the value set before.
Roll Number: 3789
Name- after: Alex