-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make variables defined in try
visible from else
#46928
Comments
I'd like to mention that |
Btw, Python's docs provides an explicit example of a new local variable for arg in sys.argv[1:]:
try:
f = open(arg, 'r')
except OSError:
print('cannot open', arg)
else:
print(arg, 'has', len(f.readlines()), 'lines')
f.close() |
In Python the variable escapes all the way, which we might not want. In [2]: try:
...: x = 1
...: except:
...: pass
...:
In [3]: x
Out[3]: 1 |
@simeonschaub bump on this, and the documentation. I tried to come up with a definitive example for the docs to PR but couldn't decide on a brief one. |
I will try to come up with some docs for this |
ref #46928 Co-authored-by: Ian Butterworth <i.r.butterworth@gmail.com>
In a
try
/else
I usually want to refer to a variable defined in thetry
but it's not available unless I putlocal x
before thetry
.The text was updated successfully, but these errors were encountered: