(I totally understand that return
only makes sense inside functions.)
My question is: given that mylist
in the following code is not None, is there a way to prevent Python from flagging the presence of return
as a syntax error, and interrupting the execution?
mylist = [1,2,3]
if mylist is None:
return []
mylist = mylist + [4]
I'm asking this because I often run chunks of code inside a function to see what it does, step by step.
I don't mind the code execution being interrupted when the condition of the if statement is met, obviously.
But I wonder if there's a way to prevent python from checking syntax errors when they are inside an if statement whose condition is not met.
Following the advice below, I'm trying to get python to ignore the syntax error using try/except:
try:
mylist=[1,2]
if mylist is None:
return []
except SyntaxError:
pass
But the execution is still interrupted with a message about the use of return
outside a function, as if the try/except was ignored.