while True:
try:
2/0
except Exception as e:
break
print e
Gives: integer division or modulo by zero
I thought scope of e
is within the while
block and it will not be accessible in the outside print
statement. What did I miss ?
Simple: while
does not create a scope in Python. Python has only the following scopes:
So when you leave the while
loop, e
, being a local variable (if the loop is in a function) or a global variable (if not), is still available.
tl;dr: Python is not C.
in except ... as e
, the e
will be drop when Jump out of try except
, Whether or not it was defined before.
When an exception has been assigned using as target, it is cleared at the end of the except clause.
refer to offical website link: https://docs.python.org/3/reference/compound_stmts.html#the-try-statement
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With