Variable scope in case of an exception in python
Simple: while
does not create a scope in Python. Python has only the following scopes:
- function scope (may include closure variables)
- class scope (only while the class is being defined)
- global (module) scope
- comprehension/generator expression scope
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