Is there a way to run a method for all instances of a class in python?
问题 Example code: >>> class MyClass(object): def __init__(self, x, y): self.x = x self.y = y def power(self): print(self.x**self.y) def divide(self): print(self.x/self.y) >>> foo = MyClass(2, 3) >>> bar = MyClass(4, 7) >>> >>> foo.power() 8 >>> bar.divide() 0.5714285714285714 Whenever I used classes in Python previously, I just ran the method for each instance separately (see above). I was just wondering If there was a way to run the same method for all the instances of that class at once,