minimum working code :
step1_failed = False
try:
print("step #1")
except:
step1_failed = True
print("step #2") # always appening after step #1 but before step #3 regardless of if step #1 failed or not
if not step1_failed:
print("step #3") # only if step #1 execute without error
My question is : is there a better way of doing this that i don't see ?
Ideally without any dummy variable like step1_failed.
I thought that maybe "finally" and "else" was the answers but finally happen after the else and i need to do something before the else statement.
The use case of this is for PyQt5, I want to disconnect a signal and reconnect it after doing something to avoid unwanted recursion. But I need to reconnect it, only if it was connected at first.
Here is my PyQt5 code to understand why i need this :
def somefunction():
connected_at_first = True # assuming it was connected
try:
lineedit1.textChanged.disconnect(somefunction) # throw a TypeError if it is not connected
except TypeError:
connected_at_first = False # happen only if lineedit1 wasn't connected
lineedit1.setText("always happening !")
# reconnecting lineedit1 if it was connected at the beginning
if connected_at_first:
lineedit1.textChanged.connect(somefunction)