I am rediscovering OOP through a GUI application I am writing and would like to understand if it is possible to change a characteristic of all objects instantiated from a class. I have the following code:
class myclass():
def __init__(self):
self.a = 1
x = myclass()
y = myclass()
print x.a, y.a # outputs 1 1
# the instruction I am looking for, assigns 2 to a in each object at once
print x.a, y.a # outputs 2 2
What I am looking for is a way to change x
and y
at once by manipulating the parent class. I know that I can have a method which modifies self.a
- I do not want to use that because it means I have to call it separately for each object.
My gut feeling is that this is not possible and that I have to cleverly handle my objects to simplify such activities (for instance by storing them in a list I would loop over, applying a method). I just do not want to miss a mechanism which I am not aware of and which would greatly simplify my code.