I am learning about classes and objects in python. I encountered a problem when I tried to create a class attribute whose value can be changed using an instance of that class. Lets assume create a class Student for students who go to the same school:
class Students:
school = "Elimu"
def __init__(self, name = "", grade= 1):
self.name = name
self.grade = grade
student_1 = Students("Imara", 5)
student_2 = Students("Jabali", 7)
I want to be able to change the class attribute using the class name and using the instance of the class i.e class_name.class_attribute = new_value class_instance.class_attribute = new_value
For further illustration(following the previous code):
Students.school
Students.school = "Ganjoni"
Students.school
student_1.school = "Vikwale"
Students.school
student_1.school
student_2.school
My desired Output:
Elimu
Ganjoni
Vikwale
Vikwale
Vikwale
Actual Output:
Elimu
Gajoni
Ganjoni
Vikwale
Ganjoni