文章详情页
[python2]local variable referenced before assignment问题
问题描述
class Test(obj):
def __init__(self): pass def _is_flag(self): try:v_sql='''Select count(*) From tablename'''push_ctrl_conn=oracle(’ETL_TEST’)self.reach_flag=push_ctrl_conn.runsql(v_sql,1)[0][0] except Exception,e:self._set_scan_state(’22’, str(e)) finally:push_ctrl_conn.close()def run(self): self._is_flag()
if name == ’__main__’:
test_case=Test()test_case.run()
类似这种格式,然后就报了UnboundLocalError: local variable ’push_ctrl_conn’ referenced before assignment
问题解答
回答1:try语法块不一定执行,即push_ctrl_conn不一定被赋值。很常见的错误,一般可以在try语法前先赋值。
像这种连接的需求,一般用上下文管理器功能,我猜测oracle库的实例也是支持的。
相关文章:
1. angular.js - 请教angularjs里html和js通信的问题2. node.js - 怎么在初始化的时候使得一个子组件不加载?3. android自带时钟应用的这个效果是怎么做的4. 代码如下,java用BigDecimal四舍五入怎么还有问题呀?哪种方式是完全没有问题的?5. javascript - 关于向java后台上传base64位字符串的图片数据,求助6. 求解决方法:Parse error: syntax error, unexpected7. 新人做网站求帮助8. 老师所用的IDE开发环境是什么?9. 请问“由于 Cookie “PHPSESSID”的“SameSite”属性设置为“None”,但缺少“Secure”属性,此 Cookie 未来将被拒绝。”请问出现这个问题怎么办?10. Help!求帮助
排行榜