这是如何“在转让前引用”?

时间:2009-04-16 01:17:44

标签: python

我有一些Python连接到一个数据库,其中有一个用于本地和实时的交换机。

    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"])

本地工作正常(Windows,Python 2.5)但是实时(Linux,Python 2.4)我得到了:

UnboundLocalError: local variable 'connection_info' referenced before assignment

即使我删除if / else并且只是直接将连接信息分配给LIVE_CONNECTION值,我也会看到相同的错误。如果我将实时连接值硬编码到最后一行,则一切正常。显然我很困。我没看到什么?

2 个答案:

答案 0 :(得分:16)

第二项作业拼写错误。

你用3 n编写了connnection_info = LIVE_CONNECTION

答案 1 :(得分:4)

错字:connnection_info = LIVE_CONNECTION