Imagine that I have the following code (This is just an example for demonstrating the thing that I want to achieve):
class Foo:
def __init__(self, name):
self.name = name
def update_something(self, number):
"""
This method is responsible for updating the object state, and it will be called hundreds of times at the same time
So updating its state last call will be enough.
"""
if number != 100:
print(f"This method has been called for: ({number}) of times ---> [{self.name}]")
else:
print(f"This is the Final call ---> [{self.name}]")
if __name__ == '__main__':
obj1 = Foo("obj1")
obj2 = Foo("obj2")
obj3 = Foo("obj3")
obj4 = Foo("obj4")
# .
# .
# .
# I have many objects
list_of_objects = [obj1, obj2, obj3, obj4]
# for example Let's imagine we have the following code
for i in range(100):
print("---")
for obj in list_of_objects:
obj.update_something(i)
The results for sure will be:
This method has been called for: (0) of times ---> [obj1]
This method has been called for: (0) of times ---> [obj2]
This method has been called for: (0) of times ---> [obj3]
This method has been called for: (0) of times ---> [obj4]
...
This method has been called for: (100) of times AND This is the Final call ---> [obj1]
This method has been called for: (100) of times AND This is the Final call ---> [obj2]
This method has been called for: (100) of times AND This is the Final call ---> [obj3]
This method has been called for: (100) of times AND This is the Final call ---> [obj4]
Instead, since it is performance-consuming, I want to take the last call in that "chain". so the update method will be called only once if they are repeatedly calling in a very very short time.
This is the Final call ---> [obj1]
This is the Final call ---> [obj2]
This is the Final call ---> [obj3]
This is the Final call ---> [obj4]