Home  >  Q&A  >  body text

[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

迷茫迷茫2741 days ago921

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:33:08

    The

    try syntax block may not be executed, that is, push_ctrl_conn may not be assigned a value. A very common mistake, generally you can assign a value before the try syntax.

    For this kind of connection requirement, the context manager function is generally used. I guess the instance of the oracle library also supports it.

    reply
    0
  • Cancelreply