[python2]local variable referenced before assignment问题

查看:164
本文介绍了[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

解决方案

try语法块不一定执行,即push_ctrl_conn不一定被赋值。很常见的错误,一般可以在try语法前先赋值。

像这种连接的需求,一般用上下文管理器功能,我猜测oracle库的实例也是支持的。

这篇关于[python2]local variable referenced before assignment问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆