So I am wondering how exactly the keyboard interrupt relates to variable assignments. I want to know specifically if in the following 2 examples there would be a difference. The difference I am expecting is that in the second case all variables will either be updated with new values or none of them will where as in the first instance some of them may be while others may not before a given interrupt. I'm not sure of this however and that is why I am asking.
while True:
try:
a = foo()
b = bar()
c = baz()
except KeyboardInterrupt:
print (a,b,c)
#vs.
def bam(a,b,c):
return foo(),bar(),baz()
while True:
try:
a,b,c = bam(a,b,c)
except KeyboardInterrupt:
print (a,b,c)
I ran the following to check things out.
a,b,c = 0,0,0
def increment(n):
return n+1
while True:
try:
a = increment(a)
b = increment(b)
c = increment(c)
except KeyboardInterrupt:
if a!=b or a!=c:
print (a,b,c)
break
#vs.
def bam(a,b,c):
return increment(a),increment(b),increment(c)
while True:
try:
a,b,c = bam(a,b,c)
except KeyboardInterrupt:
if a!=b or a!=c:
print (a,b,c)
break
It demonstrates that multiple assignment is indeed as @MichaelRuth says "non-atomic".