Local variable might be referenced before assignment

This is the place for queries that don't fit in any of the other categories.

Local variable might be referenced before assignment

Postby steadyonabix » Sat Feb 01, 2014 12:19 pm

Hi

I am using pycharm while teaching myself python and it is popping up the warning given in the title of this post.

my code is -

Code: Select all
    #====================================
    # Setup the master DB
    #====================================
    db.copy_db_to_ctl(m_vars, m_master_db_path, 'master.db')
    logging.info('Master DB copied to run directory')

    try:
        m_db_cnxn = db.Sqlite3Connection(m_master_db_path)
    except Exception, em:
        lib.error_exit('Failed to open connection to master database. Error ({0})'.format(str(em)), 'CRITICAL')

    logging.info('Success opening master DB ({0})'.format(m_master_db_path))

    m_db_cnxn.closeCnxn()

    if __name__ == '__main__':
        main()


Pycharm is underlining the final reference to m_db_cnxn and warning me that I might be referencing the local variable before assignment because it is unaware that my error_exit function is exiting the program if an exception occurs.

This code is from my main module and the object is in another called db.

Is there a way to get around this as I assume that every reference to my connection object will have the same warning.

Thanks in advance

Brad
steadyonabix
 
Posts: 6
Joined: Tue Dec 31, 2013 1:11 pm

Re: Local variable might be referenced before assignment

Postby metulburr » Sat Feb 01, 2014 12:43 pm

I would just ignore the warning, but you should be able to get rid of it by assigining the object to None before the try/except block or within the except block, thus no longer referencing a local variable before assignment
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1450
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Local variable might be referenced before assignment

Postby steadyonabix » Sat Feb 01, 2014 1:06 pm

Thanks :)

The assignment to None did the trick
steadyonabix
 
Posts: 6
Joined: Tue Dec 31, 2013 1:11 pm


Return to General Coding Help

Who is online

Users browsing this forum: 7stud, Bing [Bot] and 2 guests