为什么我的代码突然开始抛出UnboundLocalError:在赋值错误之前引用的局部变量?

时间:2016-12-25 03:59:09

标签: python python-2.7

我的代码突然开始在每个变量上抛出UnboundLocalError:

为什么突然这样做?

我是否需要在allowed_domains之外定义try:,如果是,为什么以前不会导致错误?

感谢。

File "/spiders/functions.py", line 85, in getAllowedDomains

return (allowed_domains)

UnboundLocalError: local variable 'allowed_domains' referenced before assignment

代码:

def getAllowedDomains():
    try:
        connection = lite.connect('links.db')
        c = connection.cursor()
        c.execute("SELECT site from sites")

        all_rows = c.fetchall()

        allowed_domains = []
        for row in all_rows:

            url = row[0]

            url = url[7:]
            print 'allowed url: '
            print url


            allowed_domains.append(url)

    except lite.Error, e:
        print "Error %s:" % e.args[0]

    finally:
        if connection:
            connection.close()


    return (allowed_domains)

0 个答案:

没有答案