Tom
Tom

Reputation: 22841

How is this "referenced before assignment"?

I have a bit of Python to connect to a database with a switch throw in for local versus live.

    LOCAL_CONNECTION = {"server": "127.0.0.1", "user": "root", "password": "", "database": "testing"}
    LIVE_CONNECTION = {"server": "10.1.1.1", "user": "x", "password": "y", "database": "nottesting"}

    if debug_mode:
        connection_info = LOCAL_CONNECTION
    else:
        connnection_info = LIVE_CONNECTION
    self.connection = MySQLdb.connect(host = connection_info["server"], user = connection_info["user"], passwd = connection_info["password"], db = connection_info["database"])

Works fine locally (Windows, Python 2.5) but live (Linux, Python 2.4) I'm getting:

UnboundLocalError: local variable 'connection_info' referenced before assignment

I see the same error even if I remove the if/ else and just assign connection info directly to the LIVE_CONNECTION value. If I hard-code the live connection values into the last line, it all works. Clearly I'm sleepy. What am I not seeing?

Upvotes: 1

Views: 766

Answers (2)

RossFabricant
RossFabricant

Reputation: 12492

Typo: connnection_info = LIVE_CONNECTION

Upvotes: 4

Van Gale
Van Gale

Reputation: 43922

The second assignement is misspelled.

You wrote connnection_info = LIVE_CONNECTION with 3 n's.

Upvotes: 16

Related Questions