I have some python code like this:
try:
bob = bob_module.Bob('alpha')
except Exception as e:
print 'Alpha mode failed for bob: ' + str(e) + ').'
try:
if bob:
bob.add('Beta')
else:
bob = bob_module.Bob('Beta')
except Exception as e:
print 'Beta mode failed for bob: ' + str(e) + ').'
When this code ran Alpha mode failed, for an understandable reason (interaction with alpha server failed). However beta mode failed for the reason "name 'bob' is not defined".
Surely if name bob
is not defined then if bob
equates to false, we drop into the else
clause and run the Bob
constructor assigning the result to the variable bob
?
I cannot now debug this because the error that caused Alpha mode to fail was transient and has now gone away, but I would like to understand this for two reasons: intellectual curiosity, and to make my code robust in case alpha mode starts failing again.