try / except中的值在执行后可用

时间:2017-01-31 12:25:29

标签: python python-3.x variables try-catch

我在python3中有一个关于try/except的问题。

我想知道你是否有类似的代码:

#1

try:
    #do something here
    var = 'some value here'

except:
    #do something if it fails

#2

try:
    #do something here
    newvar = var    #var from above

except:
    #do something if it fails

我可以使用#1 中的#1 var,就像我正在使用它一样,或者var的值不再是try or except进入#1 ======================================================= = Elapsed: yxz ms = In msg: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><BLRequest xmlns="http://www.wwww.ww/wwww/"> *** <find_me>12345678901</find_me> *** </BLRequest></soapenv:Body></soapenv:Envelope> = Out msg: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/" envelope:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> *** </Body></Envelope> ======================================================= ======================================================= = Elapsed: yxz ms = In msg: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><BLRequest xmlns="http://www.wwww.ww/wwww/"> *** <find_me>6545678901</find_me> *** </BLRequest></soapenv:Body></soapenv:Envelope> = Out msg: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/" envelope:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> *** </Body></Envelope> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http " > *** </SOAP-ENV:Envelope> ======================================================= ======================================================= = Elapsed: yxz ms = In msg: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><BLRequest xmlns="http://www.wwww.ww/wwww/"> *** <find_me>12345678901</find_me> *** </BLRequest></soapenv:Body></soapenv:Envelope> = Out msg: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/" envelope:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> *** </Body></Envelope> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http " > *** </SOAP-ENV:Envelope> ======================================================= 区块?

谢谢

2 个答案:

答案 0 :(得分:2)

取决于。

如果#1中的代码在计算要存储到变量var中的值时失败,则分配将永远不会发生,var将与该代码块之前一样。 Python文档明确指出,首先计算等号右侧的值,并且当计算完成时,结果值然后绑定到等号左侧的变量名。如果var未定义,则仍未定义;如果它被定义并且有一个值,它仍将具有该值。它不会失去定义或价值。

因此,如果未定义var,您将在代码块#2中获得异常。如果var具有旧值,则它仍然具有该值,并且代码块#2将使用该值。

这是否是你想要的取决于其他因素。

@Chris_Rands在评论中指出,你的风格不是很好。您应该捕获特定的异常并处理它们。应提出意外的例外情况并在更高层面处理。例外情况是,您的代码用于长时间运行的程序,除了用户关闭之外,您不希望因任何原因而停止该程序。在这种情况下,您仍然应该捕获并处理您期望的特定异常,并将意外的异常及其完整的回溯记录到某个文件中,以便程序员稍后可以检查它以解决问题。永远不要捕获所有异常,只需继续进行某种日志记录。

答案 1 :(得分:0)

如果在分配变量之前try块中发生异常,则不会声明它。

为了以后安全地使用您的变量,您还必须在发生异常时(即在except块中)设置它。