I often have the case where I use two variables, one of them being the "current" value of something, another one a "newly retrieved" one.
After checking for equality (and a relevant action taken), they are swapped. This is then repeated in a loop.
import time
import random
def get_new():
# the new value is retrieved here, form a service or whatever
vals = [x for x in range(3)]
return random.choice(vals)
current = None
while True:
# get a new value
new = get_new()
if new != current:
print('a change!')
else:
print('no change :(')
current = new
time.sleep(1)
This solution works but I feel that it is a naïve approach and I think I remember (for "write pythonic code" series of talks) that there are better ways.
What is the pythonic way to handle such mechanism?