exec()命令以普通代码启动,但不在方法内

时间:2019-07-08 14:36:14

标签: function exec python-3.7

有人可以向我解释为什么在方法外调用exec()可以正常工作,但是在方法内会引发以下错误吗?

UnboundLocalError: local variable 'connectivities' referenced before assignment

这是示例代码:

#!/usr/bin/env python

CON_FILE = "sample_input.txt"


import re
AAIG_connectivities_dict = {}
with open(CON_FILE, 'r') as f:
    for line in f:
        m = re.search("^([A-Za-z0-9]+N[DE12X]*H[DE12X]*)\s+(\(.*)$", line)
        if m:
            i_AAIG_name = m.group(1)
            s = "connectivities = " + m.group(2) + ","  ; # comma to support the single connectivities, too.
            exec(s)
            connectivities = list(connectivities)
            AAIG_connectivities_dict[i_AAIG_name] = connectivities
print(AAIG_connectivities_dict)


def load_connectivities_from_file(CON_FILE):
    import re
    AAIG_connectivities_dict = {}
    with open(CON_FILE, 'r') as f:
        for line in f:
            m = re.search("^([A-Za-z0-9]+N[DE12X]*H[DE12X]*)\s+(\(.*)$", line)
            if m:
                i_AAIG_name = m.group(1)
                s = "connectivities = " + m.group(2) + ","  ; # comma to support the single connectivities, too.
                exec(s)
                connectivities = list(connectivities)
                AAIG_connectivities_dict[i_AAIG_name] = connectivities
    print(AAIG_connectivities_dict)

load_connectivities_from_file(CON_FILE)

要生成“ sample_input.txt”,请在BASH shell中执行:

cat > sample_input.txt << EOF
i    possible i-1
X10NXHX    ('X8NXHX', 2, 3, 1.0, 0.29096014803723164), ('X15NXHX', 1, 3, 0.5075369033220299, 0.017894454516959042), ('X62NXHX', 1, 3, 0.2811455233433974, 0.009912472860959547), ('X9NXHX', 1, 3, 0.12840435222669283, 0.004527209402233843), ('X29NXHX', 1, 3, 0.10270495431102347, 0.003621114290518612), ('X13NXHX', 1, 3, 0.08843022425103912, 0.0031178237787778664), ('X27NXHX', 1, 3, 0.06967097761433207, 0.0024564206699284994), ('X61NXHX', 1, 3, 0.05203985561001743, 0.001834792353398024)
X12NXHX    ('X11NXHX', 1, 4, 1.0, 0.014683671473740228), ('X32NXHX', 1, 4, 0.4498988189862975, 0.006606166454418515), ('X31NXHX', 1, 4, 0.1633848174540025, 0.0023990889832915907), ('X36NXHX', 1, 4, 0.08439509702204087, 0.0012392298786660803), ('X13NXHX', 1, 4, 0.049958867050537537, 0.0007335795909703586), ('X30NXHX', 1, 4, 0.02502771889502097, 0.00036749880199160877), ('X38NXHX', 1, 4, 0.02304040021466484, 0.00033831766737563233), ('X40NXHX', 1, 4, 0.01903733838008644, 0.00027953802250761526), ('X29NXHX', 1, 4, 0.01757512963793901, 0.00025806742971189146), ('X5NXHX', 1, 4, 0.01709261655271193, 0.0002509823660866362), ('X39NXHX', 1, 4, 0.007533665145110988, 0.00011062186398397724), ('X33NXHX', 1, 4, 0.005975869974827519, 8.774771148015558e-05), ('X41NXHX', 1, 4, 0.00046841775328956294, 6.878092401771443e-06), ('X55NXHX', 1, 4, 0.00035046651402599333, 5.146135154504658e-06), ('X35NXHX', 1, 4, 8.313139742956269e-05, 1.2206741290086314e-06)
EOF

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。在Python 3中,必须在exec()中使用新的空名称空间。因此代码应如下所示:

#!/usr/bin/env python

CON_FILE = "sample_input.txt"


import re
AAIG_connectivities_dict = {}
with open(CON_FILE, 'r') as f:
    for line in f:
        m = re.search("^([A-Za-z0-9]+N[DE12X]*H[DE12X]*)\s+(\(.*)$", line)
        if m:
            i_AAIG_name = m.group(1)
            s = "connectivities = " + m.group(2) + ","  ; # comma to support the single connectivities, too.
            exec(s)
            connectivities = list(connectivities)
            AAIG_connectivities_dict[i_AAIG_name] = connectivities
print(AAIG_connectivities_dict)


def load_connectivities_from_file(CON_FILE):
    import re
    AAIG_connectivities_dict = {}
    with open(CON_FILE, 'r') as f:
        for line in f:
            m = re.search("^([A-Za-z0-9]+N[DE12X]*H[DE12X]*)\s+(\(.*)$", line)
            if m:
                i_AAIG_name = m.group(1)
                s = "connectivities = " + m.group(2) + ","  ; # comma to support the single connectivities, too.
                namespace = {}
                exec(s, namespace)
                connectivities = list(namespace['connectivities'])
                AAIG_connectivities_dict[i_AAIG_name] = connectivities
    print(AAIG_connectivities_dict)

load_connectivities_from_file(CON_FILE)