<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">On 1/6/19 9:14 AM, Steven D'Aprano
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:20190106141453.GY13616@ando.pearwood.info">[...]<br>
<pre class="moz-quote-pre" wrap="">But a better question is, why would you (generic, not you personally)
imagine that, alone out of all flow control statements, ONLY "except"
clauses introduce a new scope? Every other flow control statement (for,
while, if, elif, else, try, with) runs in the current scope. The only
statements which create a new scope are def and class. (Did I miss any?)
</pre>
</blockquote>
<p>To be fair except is already unique in that it <i>does</i>
"pseudo-scope" the binding to the variable. The other obvious
comparisons are to for loops and context managers, both of which
bind a value to a name that survives after the exit of the control
flow statement.<br>
</p>
<p>Given the reference counting reasons for exceptions *not* to
outlive their control flow statement, they are the "odd man out"
in that they delete the exception after the control statement's
body exits. To me, the natural and intuitive way to do this would
be to have the exception live in its own scope where it shadows
existing variables, rather than replacing and then completely
removing them. The way it works now is halfway between the
behavior of existing control flow statements and having a proper
nested scope.</p>
<p>Not saying that anything has to change - in the end this is one
of the more minor "gotchas" about Python, and there may be
practical reasons for leaving it as it is - but I do think it's
worth noting that for many people this <i>will</i> be surprising
behavior, even if you know that exceptions don't survive outside
of the "except" block.<br>
</p>
</body>
</html>