Python 如何在 try/except 块中公开变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25666853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a variable inside a try/except block public?
提问by x0x
How can I make a variable inside the try/except block public?
如何在 try/except 块中公开变量?
import urllib.request
try:
url = "http://www.google.com"
page = urllib.request.urlopen(url)
text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
print("Unable to process your request dude!!")
print(text)
This code returns an error
此代码返回错误
NameError: name 'text' is not defined
NameError:未定义名称“文本”
How can I make the variable text available outside of the try/except block?
如何使变量文本在 try/except 块之外可用?
采纳答案by chepner
trystatements do not create a new scope, but textwon't be set if the call to url lib.request.urlopenraises the exception. You probably want the print(text)line in an elseclause, so that it is only executed when there is no exception.
try语句不会创建新范围,但text如果调用url lib.request.urlopen引发异常,则不会设置。您可能希望print(text)在else子句中使用该行,以便仅在没有异常时才执行该行。
try:
url = "http://www.google.com"
page = urllib.request.urlopen(url)
text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
print("Unable to process your request dude!!")
else:
print(text)
If textneeds to be used later, you really need to think about what its value is supposed to be if the assignment to pagefails and you can't call page.read(). You can give it an initial value prior to the trystatement:
如果以后text需要使用,你真的需要考虑如果赋值page失败并且你不能调用它的值应该是什么page.read()。你可以在try语句之前给它一个初始值:
text = 'something'
try:
url = "http://www.google.com"
page = urllib.request.urlopen(url)
text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
print("Unable to process your request dude!!")
print(text)
or in the elseclause:
或在else条款中:
try:
url = "http://www.google.com"
page = urllib.request.urlopen(url)
text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
print("Unable to process your request dude!!")
else:
text = 'something'
print(text)
回答by Nishant Nawarkhede
Just declare the variable textoutside tryexceptblock,
只需text在tryexcept块外声明变量,
import urllib.request
text =None
try:
url = "http://www.google.com"
page = urllib.request.urlopen(url)
text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
print("Unable to process your request dude!!")
if text is not None:
print(text)
回答by 4xy
As answered before there is no new scope introduced by using try exceptclause, so if no exception occurs you should see your variable in localslist and it should be accessible in current (in your case global) scope.
正如之前回答的那样, usingtry except子句没有引入新的范围,因此如果没有发生异常,您应该在locals列表中看到您的变量,并且它应该可以在当前(在您的情况下为全局)范围内访问。
print(locals())
In module scope (your case) locals() == globals()
在模块范围内(您的情况) locals() == globals()

