Search⌘ K
AI Features

The Disappearing Variable From Outer Scope

Explore how Python handles variables assigned in exception clauses and why such variables may disappear from outer scopes. Understand the implications of exception variable clearance and learn to manage variable scopes effectively to write more reliable Python code.

We'll cover the following...

Can Python make a variable vanish? Some might think so.

⚠️ The following code is meant for Python 2.x versions.

Python
e = 7
try:
raise Exception()
except Exception as e:
pass
print(e)

⚠️ The following code is meant for Python 3.x versions.

Python 3.5
e = 7
try:
raise Exception()
except Exception as e:
pass
print(e)

Explanation

  • According to the ...